import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.apache.http.util.EntityUtils;
import android.util.Log;
public class GetPostEx {
public String getInternetData() throws Exception{
//String source_prog;
//int lang_number;
//BufferedReader in = null;
String ret=null;
try{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("Some server link");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("source", "print 1"));
nameValuePairs.add(new BasicNameValuePair("lang", "5"));
nameValuePairs.add(new BasicNameValuePair("testcases", "[\"1\"]"));
nameValuePairs.add(new BasicNameValuePair("api_key", "Some key"));
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.e("error", "before HTTPResponse");
HttpResponse response = client.execute(request);
Log.e("error", "Aftr HTTPResponse");
ret = EntityUtils.toString(response.getEntity());
}catch (Exception e){
//Log.e("error","error!!");
e.printStackTrace();
}
return ret;
}
}
and the code used in the other activity(AndroidHTTPRequestsActivity) from where i am calling my GetPostEx class is:-
GetPostEx test = new GetPostEx();
try{
returned = test.getInternetData();
} catch(Exception e){
Log.e("error", "yo") ;
e.printStackTrace();
}
I am trying to make an android application which needs some HTTP Response from a server using API.This is my code for getting an HTTP response.But the execution halts after the line HttpResponse response = client.execute(request); The logcat is not displaying the error after this line whereas the one above it is getting displayed. I am unable to figure out the reason behind this.
The following lines are the stack trace details in logcat:-
12-22 14:18:58.658: E/error(1335): before HTTPResponse
12-22 14:18:59.448: W/System.err(1335): android.os.NetworkOnMainThreadException
12-22 14:18:59.448: W/System.err(1335): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-22 14:18:59.458: W/System.err(1335): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-22 14:18:59.458: W/System.err(1335): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-22 14:18:59.458: W/System.err(1335): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-22 14:18:59.458: W/System.err(1335) : at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
12-22 14:18:59.458: W/System.err(1335): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-22 14:18:59.458: W/System.err(1335): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-22 14:18:59.478: W/System.err(1335): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-22 14:18:59.478: W/System.err(1335): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-22 14:18:59.478: W/System.err(1335): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-22 14:18:59.478: W/System.err(1335): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-22 14:18:59.478: W/System.err(1335): at com.us.onlinecompilr.GetPostEx.getInternetData(GetPostEx.java:37)
12-22 14:18:59.478: W/System.err(1335): at com.us.onlinecompilr.AndroidHTTPRequestsActivity.onCreate(AndroidHTTPRequestsActivity.java:29)
12-22 14:18:59.478: W/System.err(1335): at android.app.Activity.performCreate(Activity.java:5243)
12-22 14:18:59.478: W/System.err(1335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 14:18:59.478: W/System.err(1335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-22 14:18:59.488: W/System.err(1335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-22 14:18:59.488: W/System.err(1335): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-22 14:18:59.488: W/System.err(1335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-22 14:18:59.488: W/System.err(1335): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 14:18:59.488: W/System.err(1335): at android.os.Looper.loop(Looper.java:137)
12-22 14:18:59.488: W/System.err(1335): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-22 14:18:59.488: W/System.err(1335): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 14:18:59.488: W/System.err(1335): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 14:18:59.488: W/System.err(1335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-22 14:18:59.488: W/System.err(1335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-22 14:18:59.488: W/System.err(1335): at dalvik.system.NativeStart.main(Native Method)
12-22 14:18:59.588: I/Choreographer(1335): Skipped 641 frames! The application may be doing too much work on its main thread.
12-22 14:19:03.278: I/Choreographer(1335): Skipped 33 frames! The application may be doing too much work on its main thread.
12-22 14:19:06.188: I/Choreographer(1335): Skipped 112 frames! The application may be doing too much work on its main thread.