java - DefaultHttpClient Depricated Android Studio 2.2.2 -
after following few guides on how data , send data database on local server using apache , mysqlli , error occurred saying code deprecated. after looking guides on deprecation found out of them outdated. here codes of manifest, parser , , gradle file module(by order):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.denis.loginappphpmyadmintest" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".mainactivity" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
package com.example.denis.loginappphpmyadmintest; import android.content.contentvalues; import android.util.log; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, contentvalues params) { // making http request try { // check request method if(method.equals("post")){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); }else if(method.equals("get")){ // request method defaulthttpclient httpclient = new defaulthttpclient(); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; httpget httpget = new httpget(url); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
apply plugin: 'com.android.application' android { compilesdkversion 25 buildtoolsversion "25.0.0" defaultconfig { applicationid "com.example.denis.loginappphpmyadmintest" minsdkversion 23 targetsdkversion 25 versioncode 1 versionname "1.0" testinstrumentationrunner "android.support.test.runner.androidjunitrunner" } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } } android { uselibrary 'org.apache.http.legacy' } dependencies { compile filetree(dir: 'libs', include: ['*.jar']) androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' testcompile 'junit:junit:4.12' }
what steps need take in order solve problem? in urlencodedformentity can't receive contentvalues object , how solve too?
[update] highly recommend use okhttp instead of org.apache.http
. way faster (coding time) , simpler maintain/code.
get request
okhttpclient client = new okhttpclient(); string run(string url) throws ioexception { request request = new request.builder() .url(url) .build(); response response = client.newcall(request).execute(); return response.body().string(); }
post request
public static final mediatype json = mediatype.parse("application/json; charset=utf-8"); okhttpclient client = new okhttpclient(); string post(string url, string json) throws ioexception { requestbody body = requestbody.create(json, json); request request = new request.builder() .url(url) .post(body) .build(); response response = client.newcall(request).execute(); return response.body().string(); }
have'nt find better far!
just add following dependency
compile 'com.squareup.okhttp3:okhttp:3.4.2'
the full code.
import android.content.contentvalues; import android.util.log; import org.json.jsonexception; import org.json.jsonobject; import java.io.ioexception; import okhttp3.mediatype; import okhttp3.okhttpclient; import okhttp3.request; import okhttp3.requestbody; import okhttp3.response; public class jsonparser { static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public static jsonobject makehttprequest(string url, string method, contentvalues params) { // making http request try { final okhttpclient client = new okhttpclient(); request request; // check request method if (method.equals("post")) { // request method post final mediatype json = mediatype.parse("application/json; charset=utf-8"); final requestbody body = requestbody.create(json, params.tostring()); request = new request.builder() .url(url) .post(body) .build(); } else if (method.equals("get")) { // request method request = new request.builder() .url(url) .build(); } final response response = client.newcall(request).execute(); json = response.body().string(); } catch (ioexception e) { e.printstacktrace(); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e ){ log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
Comments
Post a Comment