I am new in android development. I want to call Api Url in android Studio. I am not able to call api url in android please help
Asked
Active
Viewed 1,388 times
2
-
4Please post the code that you've tried so far. – Mohammad Tauqir Dec 28 '15 at 08:08
-
No one can help you because of too short description of your problem. Provide proper description of your problem and tried code. – Chirag Savsani Dec 28 '15 at 08:09
-
Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read [FAQ](http://stackoverflow.com/tour), [How to Ask](http://stackoverflow.com/help/how-to-ask) and [help center](http://stackoverflow.com/help) as a start. – Nahuel Ianni Dec 28 '15 at 08:27
1 Answers
0
Just a stab in the dark since I don't know what you are trying to do. Here is an AsyncTask command I used called SendArrival.java:
SendArrival.java
package company.com.mirrorandroid.commands;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import company.com.mirrorandroid.model.Model;
public class SendArrival extends AsyncTask<Void, Integer, Integer> {
private Context context;
public SendArrival(Context context) {
this.context = context;
}
protected void onPreExecute() {
//
}
@Override
protected void onPostExecute(Integer success) {
Log.d("Mirror", "finished " + success.toString());
}
@Override
protected Integer doInBackground(Void... Void) {
try {
URL url = new URL("http://foo.com/arrive");
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("foo.com/arrive");
try {
SharedPreferences sp = context.getSharedPreferences(Model.PREFS_NAME, 0);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("deviceId", "12345"));
nameValuePairs.add(new BasicNameValuePair("guestName", sp.getString("guestName","Android Man")));
nameValuePairs.add(new BasicNameValuePair("message", sp.getString("message","Better than iOS")));
nameValuePairs.add(new BasicNameValuePair("memberType", sp.getString("memberType","Green")));
nameValuePairs.add(new BasicNameValuePair("handle", sp.getString("handle","no-handle from Android")));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d("mirror", response.toString());
if(response.getStatusLine().getStatusCode() != 200){
Log.d("mirror", "error occurred " + response.getStatusLine().getReasonPhrase());
return 0;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e("mirror", "Send Arrival client prototcol error " + e.toString());
return 0;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("mirror", "Send Arrival io error " + e.toString());
return 0;
}
} catch (Exception e) {
Log.d("sp", e.getMessage());
return 0;
}
return 1;
}
}
Then you can invoke it from your activity like this:
new SendArrival(context).execute();
Like the other commenters, give some more details and we can help you.

b-ryce
- 5,752
- 7
- 49
- 79