Networkonmainthreadexception On Higher Api
I am having a script force close only when i debug on higher API : 16 , But it is working fine when it come to an API : 10. Could it be my project setup problem? It is a simple re
Solution 1:
You have a NetworkOnMainThreadException
which occurs on the new versions of Android (3.0+) if you try to do network operations on the main (UI) Thread (StrictMode
),. Use an AsyncTask
for your network operations, it's simple to set up, and operates in a logical manner (execute something in background then once you're done, publish to UI Thread).
Solution 2:
Try this, use AsyncTask
method it will resolve ur problem..
protectedclassGetTaskextendsAsyncTask<Void, Void, Integer> {
protectedvoidonPreExecute() {
ProgressDialogmProgressDialog= ProgressDialog.show(MainActivity.this,
"", "Finding cars");
}
@Overrideprotected Integer doInBackground(Void... params) {
// TODO Auto-generated method stub//Do your stuffreturn0;
}
protectedvoidonPostExecute(Integer result) {
super.onPostExecute(result);
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
Solution 3:
In case , someone go into this situation . i decided to post my working code
publicclassgalleryextendsFragment {
JSONArray jArray;
Stringresult=null;
InputStreamis=null;
StringBuildersb=null;
private ListView storeList;
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stubViewmyFragmentView= inflater.inflate(R.layout.tab_frag1_layout,
container, false);
storeList = (ListView) inflater.inflate(R.layout.list, null);
return myFragmentView;
}
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
}
@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);
newtask().execute();
}
classtaskextendsAsyncTask<String, String, Void> {
privateProgressDialogprogressDialog=newProgressDialog(getActivity());
InputStreamis=null;
Stringresult="";
protectedvoidonPreExecute() {
progressDialog.setMessage("Download data...");
progressDialog.show();
}
@Overrideprotected Void doInBackground(String... params) {
Stringurl_select="http://xxxxxxxxxxxx/android_link.php";
HttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost(url_select);
ArrayList<NameValuePair> param = newArrayList<NameValuePair>();
try {
httpPost.setEntity(newUrlEncodedFormEntity(param));
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReaderbr=newBufferedReader(
newInputStreamReader(is));
StringBuildersb=newStringBuilder();
Stringline="";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result " + e.toString());
}
returnnull;
}
protectedvoidonPostExecute(Void v) {
// ambil data dari Json databasetry {
JSONArrayJarray=newJSONArray(result);
for (inti=0; i < Jarray.length(); i++) {
JSONObjectJasonobject=null;
Jasonobject = Jarray.getJSONObject(i);
// get an output on the screenStringbid= Jasonobject.getString("brand_id");
Stringsname= Jasonobject.getString("series_name");
}
this.progressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}
}
Post a Comment for "Networkonmainthreadexception On Higher Api"