Reputation: 45
I am trying to make http request.
I am using HTTP Parsing.
Below is the code
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "[email protected]"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
I get runtime error. Below is the Logcat.
It gives some Fatal Exception.
07-08 17:17:37.866: E/AndroidRuntime(26380): FATAL EXCEPTION: main
07-08 17:17:37.866: E/AndroidRuntime(26380): android.os.NetworkOnMainThreadException
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
07-08 17:17:37.866: E/AndroidRuntime(26380): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.net.Socket.connect(Socket.java:857)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-08 17:17:37.866: E/AndroidRuntime(26380): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.custom.functions.citycoupons.CustomDialog$7.onClick(CustomDialog.java:465)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.view.View.performClick(View.java:4091)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.view.View$PerformClick.run(View.java:17072)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Handler.handleCallback(Handler.java:615)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.os.Looper.loop(Looper.java:153)
07-08 17:17:37.866: E/AndroidRuntime(26380): at android.app.ActivityThread.main(ActivityThread.java:5086)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 17:17:37.866: E/AndroidRuntime(26380): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-08 17:17:37.866: E/AndroidRuntime(26380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-08 17:17:37.866: E/AndroidRuntime(26380): at dalvik.system.NativeStart.main(Native Method)
07-08 17:17:39.336: E/Trace(26551): error opening trace file: No such file or directory (2)
How to fix this issue? Any help will be appreciated. Thanks in advance.
Upvotes: 0
Views: 1253
Reputation: 4487
After 2.2, you cannot call network operations on main thred, that's why it is showing NetworkOnMainThreadException
. To fix this issue, use AsyncTask class
For example,
Call this from main thread i.e after setContentView(),
new LongTask(your_activity.this).execute();
private class LongTask extends AsyncTask<Void, Void, String>
{
protected void doInBackground(Void... params) // inside this method do your network operations
{
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "[email protected]"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
return response.toString();
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
return "Error"
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
return "Error"
}
}
protected void onPostExecute(String s)
{
super.onPostExecute(s);
// here with String "s", you can access the result you got from service
}
}
Please check AsyncTask
Upvotes: 0
Reputation: 2534
Use the Asyn Task for sending the HTTP request. as you are making the request on the main thread, hence it is giving you the NetworkOnMainThreadException
on your Sticky Mode.
Upvotes: 0
Reputation: 3415
You can not make an HTTP request in the UIThread. Use AsyncTask or other threads.
new AsyncTask<Void, Void, Void>() {
String token;
@Override
protected Void doInBackground(Void... params) {
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("url");
//array NameValuePair;
// Building post parameters, key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "[email protected]"));
nameValuePair.add(new BasicNameValuePair("id", "2222"));
nameValuePair.add(new BasicNameValuePair("text", "text"));
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}.execute();
Upvotes: 1