aysnc任务中logcat文件中的json错误
public class MainActivity extends AppCompatActivity {
public TextView textView;public Button clickMe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickMe = (Button)findViewById(R.id.clickMe);
clickMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JsonClass().execute("www.webbanknepal.com/app/services.php");
}});
}
class JsonClass extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection=null;
BufferedReader reader=null;
try {
URL url= new URL(params[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream stream=connection.getInputStream();
InputStreamReader isr= new InputStreamReader(stream);
reader =new BufferedReader(isr);
StringBuffer buffer=new StringBuffer();
String line="";
while((line=reader.readLine())!=null)
{
buffer.append(line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(connection!=null){ connection.disconnect();}
try {
if(reader!=null){ reader.close();};
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
textView= (TextView)findViewById(R.id.textView);
textView.setText(s.toString());
}
}
这是我的问题
我得到的应用程序不幸紧靠这里是我的日志猫文件致命的例外
在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)处执行doInBackground()at a.done(AsyncTask.java:300)时出现错误,位于java.util.concurrent.FutureTask.setException(FutureTask.java :222)at java.util.concurrent.FutureTask.run(FutureTask.java:242)at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor .java:1112)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)at java.lang.Thread.run(Thread.java:841)引起:java.lang.NullPointerException at com。 example.shailendra.fulljson.MainActivity $ JsonClass.doInBackground(MainActivity.java:81)at com.example.shailendra.fulljson.MainActivity $ JsonClass.doInBackground(MainActivity.java:47)at android.os.AsyncTask $ 2.call(AsyncTask .java:288)at java.util.concurrent.FutureTask.run(FutureTask.java:237)at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)at java.util.concurrent.ThreadPoolExec uu.runWorker(ThreadPoolExecutor.java:1112)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)at java.lang.Thread.run(Thread.java:841)
我改变了一点你的代码,它工作正常。 检查代码....
public class MainActivity extends AppCompatActivity { public TextView textView;public Button clickMe;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickMe= (Button)findViewById(R.id.clickMe);clickMe.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
new JsonClass().execute("http://www.webbanknepal.com/app/services.php");
}
});
}
class JsonClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(stream);
reader = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(s.toString());
}
}
}
您为onPostExecute(String s)传递空字符串。
做这个:
public class MainActivity extends AppCompatActivity
{
public TextView textView;
public Button clickMe;
public static String s;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
...
new JsonClass().execute("www.webbanknepal.com/app/services.php",null,s);
}});
}
class JsonClass extends AsyncTask<String,Void,String>
{ @Override
protected String doInBackground(String... params) {
...
...
String line="";
while((line=reader.readLine())!=null)
{
buffer.append(line);
}
s=buffer.toString();
return s;
链接地址: http://www.djcxy.com/p/78425.html
上一篇: json error in logcat file in aysnc task
下一篇: getDefaultSharedPreferences(getActivity()) in AsyncTask