错误http连接android.os.networkonmainthreadexception [英] Error in http connection android.os.networkonmainthreadexception
本文介绍了错误http连接android.os.networkonmainthreadexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写的应用程序为Android,我有一个问题。它适用于手机,但它不能在平板电脑工作。
我有错误:http连接android.os.networkonmainthreadexception错误。这是我的code:
公共类AktualizacjaActivity {
公共静态最后弦乐KEY_121 =HTTP://xxx.php
公共字符串getServerData(字符串returnString,上下文的背景下){
DatabaseAdapter DB1 =新DatabaseAdapter(背景);
db1.open();
字符串的问题=;
尝试
{ 光标C = db1.makeCursor(DatabaseAdapter.STRUCT,新的String [] {} DatabaseAdapter.ID,DatabaseAdapter.KODE_ID +='+ returnString +',NULL);
c.moveToFirst();
问题+ =NOT IN(';
而(!c.isLast())
{
问题+ = c.getString(c.getColumnIndex(DatabaseAdapter.ID))+',';
c.moveToNext();
}
问题+ = c.getString(c.getColumnIndex(DatabaseAdapter.ID))+'); }
赶上(例外五)
{ } db1.close();
如果(question.equalsIgnoreCase(NOT IN('))的问题=;
InputStream为= NULL; 字符串结果=;
//一年要发送的数据,warunek韩国większyOD 1980年
ArrayList的<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;();
nameValuePairs.add(新BasicNameValuePair(code_ID,returnString));
nameValuePairs.add(新BasicNameValuePair(否,问题)); // HTTP POST
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(KEY_121);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
}赶上(例外五){
Log.e(log_tag,在HTTP连接错误+ e.toString());
} //响应转换为字符串
尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-2),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
结果= sb.toString(); }赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
}
//解析JSON数据
DatabaseAdapter DB =新DatabaseAdapter(背景);
db.open();
尝试{
JSONArray jArray =新JSONArray(结果); 的for(int i = 0; I< jArray.length();我++){
JSONObject的json_data = jArray.getJSONObject(I) db.addItmem(INSERT INTO my_kodeks_struktura VALUES(
+ json_data.getInt(ID)+,
+ json_data.getInt(code_ID)+,+
json_data.getInt(position_art)+,+
json_data.getString(position_art_a)+',+
json_data.getInt(电平)+,+
json_data.getString(名称)+');); }
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}
db.close();
返回UPD}}
解决方案
这是作为异常说:你不能对UI线程HTTP调用。至少不会在较新的操作系统。
考虑使用AsyncTask的此类调用。
I wrote app for android and i have one problem. It works on phone, but it doesn't work on tablet. I have Error : Error in http connection android.os.networkonmainthreadexception. It is my code:
public class AktualizacjaActivity { public static final String KEY_121 = "http://xxx.php";
public String getServerData(String returnString,Context context) {
DatabaseAdapter db1 = new DatabaseAdapter(context);
db1.open();
String question="";
try
{
Cursor c = db1.makeCursor(DatabaseAdapter.STRUCT, new String[] {DatabaseAdapter.ID},DatabaseAdapter.KODE_ID+"='"+returnString+"'",null);
c.moveToFirst();
question+="NOT IN('";
while(!c.isLast())
{
question+=c.getString(c.getColumnIndex(DatabaseAdapter.ID))+"','";
c.moveToNext();
}
question+=c.getString(c.getColumnIndex(DatabaseAdapter.ID))+"')";
}
catch(Exception e)
{
}
db1.close();
if(question.equalsIgnoreCase("NOT IN('")) question="";
InputStream is = null;
String result = "";
//the year data to send, warunek rok większy od 1980
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("code_id",returnString));
nameValuePairs.add(new BasicNameValuePair("no", question));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"ISO-8859-2"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
DatabaseAdapter db = new DatabaseAdapter(context);
db.open();
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
db.addItmem("INSERT INTO my_kodeks_struktura VALUES("
+json_data.getInt("id")+","
+json_data.getInt("code_id")+","+
json_data.getInt("position_art")+",'"+
json_data.getString("position_art_a")+"',"+
json_data.getInt("level")+",'"+
json_data.getString("name")+"');");
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
db.close();
return "Upd";
}
}
解决方案
it's as the exception says: you cannot make http calls on ui thread. at least not on newer OS.
consider using AsyncTask for such calls.
这篇关于错误http连接android.os.networkonmainthreadexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文