Hello I'm new in Android programming. I want to make app that can connect Android to the web server. Here's the code that I use:
public void cari (View v){
ArrayList<NameValuePair> arData = new ArrayList<NameValuePair>();
arData.add(new BasicNameValuePair("nama", edNama.getText().toString()));
String respon = null;
try {
respon = KoneksiHTTP.eksekusiHttpPost("http://example.com/myphp.php", arData);
String r = respon.toString();
r = r.trim();
AlertDialog close = new AlertDialog.Builder(this)
.setTitle("ESTIMASI MANFAAT")
.setMessage(r.toString())
.setNegativeButton("Kembali", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg, int sumthin) {
// TODO Auto-generated method stub
}
})
.show();
} catch (Exception e) {
e.printStackTrace();
}
finally {
edNama.setText(null);
}
}
and this another class
public class KoneksiHTTP {
public static final int HTTP_TIMEOUT = 3000;
private static HttpClient client;
private static HttpClient getHttpClient() {
if (client == null) {
client = new DefaultHttpClient();
final HttpParams parameterHttp = client.getParams();
HttpConnectionParams.setConnectionTimeout(parameterHttp, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(parameterHttp, HTTP_TIMEOUT);
}
return client;
}
public static String eksekusiHttpPost(String url, ArrayList<NameValuePair> postParameter) throws Exception {
BufferedReader in = null;
try {
HttpClient klien = getHttpClient();
HttpPost req = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameter);
req.setEntity(formEntity);
HttpResponse resp = klien.execute(req);
in = new BufferedReader(new InputStreamReader(resp.getEntity()
.getContent()));
StringBuffer stringBuff = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
stringBuff.append(line + NL);
}
in.close();
String hasil = stringBuff.toString();
return hasil;
} finally {
if (in != null) {
in.close();
}
}
}
}
But I got deprecated on
HttpParams, HttpConnectionParams, ConnManagerParams, setTimeout.
I've been adding
useLibrary 'org.apache.http.legacy'
but it has no effect. What should I used instead ?
here's my build gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "com.example.plz.taspenmobile"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'org.apache.httpcomponents:httpclient:4.5'
}
code for showing respon:
try {
CallWebService respon = new CallWebService("POST", "http://example.com/myphp.php", apiParams) {
@Override
public void OnStartingService() {
super.OnStartingService();
}
//@Override
public void OnGettingResult(JSONObject jsonObject) throws JSONException, MethodNotDefinedException {
super.OnGettingResult(toString());
}
};
String r = respon.toString();
r = r.trim();
AlertDialog close = new AlertDialog.Builder(estimasi.this)
.setTitle("Estimasi Manfaat")
.setMessage(r.toString())
.setNegativeButton("Kembali", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dlg, int sumthin) {
// TODO Auto-generated method stub
}
})
.show();
} catch (Exception e) {
e.printStackTrace();
}
finally {
edNama.setText(null);
}