网络OnMain线程异常 [英] Network OnMain Thread Exception
本文介绍了网络OnMain线程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。 。
MainActivity.java
Hi. .
MainActivity.java
package com.javapapers.android.form;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
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.json.JSONObject;
import com.javapapers.android.form.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class FrmActivity extends Activity {
Button mButton;
EditText mEdit;
TextView mText;
EditText mDate;
String id;
InputStream is;
String line;
String result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frm);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mEdit = (EditText)findViewById(R.id.editText1);
mText = (TextView)findViewById(R.id.textView1);
id = ((EditText) findViewById(R.id.editText2)).getText().toString();
System.out.println(id);
select();
}
});
}
public void select()
{
ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.3/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
String name = (json_data.getString("name"));
Toast.makeText(getBaseContext(), "Name : "+name,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_frm, menu);
return true;
}
}
activity_main.xml
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<edittext
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:padding="11dp"
android:hint="Id"
android:ems="10"
android:inputType="number" >
</edittext>
<requestfocus />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:padding="11dp"
android:text="Select" />
</relativelayout>
这是我的代码。 。并显示以下错误
09-06 16:56:09.414:E / Fail 1(7064):android.os.NetworkOnMainThreadException
09-06 16:56 :09.424:E / Fail 2(7064):java.lang.NullPointerException
09-06 16:56:09.434:E / Fail 3(7064):java.lang.NullPointerException
请任何人解决这个问题。 。
this my code. . and its shows the following error
09-06 16:56:09.414: E/Fail 1(7064): android.os.NetworkOnMainThreadException
09-06 16:56:09.424: E/Fail 2(7064): java.lang.NullPointerException
09-06 16:56:09.434: E/Fail 3(7064): java.lang.NullPointerException
pls anyone resolve this. .
推荐答案
不要在主线程上执行长时间运行操作。网络操作被认为是长时间运行的,如果你尝试这个,框架就会抛出(就像你刚刚经历的那样)。
创建一个AsyncTask <改为/ code>并将网络代码委托给它。
AsyncTask API文档 [ ^ ]
希望这会有所帮助,
Fredrik
Don't do long running operations on your Main thread. Network operations are considered long running and the framework will throw (like you've just experienced) if you try this.
Create anAsyncTask
and delegate the network code to that instead.
AsyncTask API documentation[^]
Hope this helps,
Fredrik
这篇关于网络OnMain线程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文