如何删除JSON解析错误 [英] How can remove JSON parsing Error

查看:84
本文介绍了如何删除JSON解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i write the below code to get data from WebAPI but it through the following errors how can remove this...
and one more thing that which IP address use for URL in genymotion Emulator
>> Caused by: java.lang.NullPointerException
>> at com.example.finalproject.Login$Myclass.doInBackground(Login.java:88)










package com.example.finalproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
//import java.net.URI;
import java.net.URL;
//import java.net.URLConnection;
import java.nio.charset.MalformedInputException;
//import java.security.PublicKey;

//import android.R.string;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.util.Log;
//import android.view.Menu;
//import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Login extends Activity {
	
	TextView tvget;
	Button btnget;
	@Override
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		btnget=(Button)findViewById(R.id.btnjson);
		tvget=(TextView)findViewById(R.id.tvjson);
		
		btnget.setOnClickListener(new View.OnClickListener() {@Override
		public void onClick(View v) {
		//	Log.i("Error", "It's working");
			new Myclass().execute("http://192.168.1.201/newAPI/api/user");
		}
			
			
		});
		
	}		
		

	public class Myclass extends AsyncTask<String, String, 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 instrm=connection.getInputStream();
	reader=new BufferedReader(new InputStreamReader(instrm));
	StringBuffer b=new StringBuffer() ;
	//String line="";
	while (reader.readLine()!=null) 
	{//line=reader;
		b.append(reader);

	}
	
	return b.toString();
	
	
		} 
			catch (MalformedInputException e) {
			e.printStackTrace();
		}
		
			catch (IOException e) {
		e.printStackTrace();	
		
		}
		finally {
			connection.disconnect();
			try {
				reader.close();
			} catch (IOException e2) {
				
			e2.printStackTrace();
			}
		}
		return null;
		
		
	}	//doinbackground method body end
				
		@Override
		protected void onPostExecute(String result) {
			
			super.onPostExecute(result);
					
			tvget.setText(result);
		}
	}//jsontask class body end
	
	}

推荐答案

Myclass.doInBackground(Login.java:88)
Myclass.doInBackground(Login.java:88)










package com.example.finalproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
//import java.net.URI;
import java.net.URL;
//import java.net.URLConnection;
import java.nio.charset.MalformedInputException;
//import java.security.PublicKey;

//import android.R.string;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.util.Log;
//import android.view.Menu;
//import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Login extends Activity {
	
	TextView tvget;
	Button btnget;
	@Override
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		btnget=(Button)findViewById(R.id.btnjson);
		tvget=(TextView)findViewById(R.id.tvjson);
		
		btnget.setOnClickListener(new View.OnClickListener() {@Override
		public void onClick(View v) {
		//	Log.i("Error", "It's working");
			new Myclass().execute("http://192.168.1.201/newAPI/api/user");
		}
			
			
		});
		
	}		
		

	public class Myclass extends AsyncTask<String, String, 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 instrm=connection.getInputStream();
	reader=new BufferedReader(new InputStreamReader(instrm));
	StringBuffer b=new StringBuffer() ;
	//String line="";
	while (reader.readLine()!=null) 
	{//line=reader;
		b.append(reader);

	}
	
	return b.toString();
	
	
		} 
			catch (MalformedInputException e) {
			e.printStackTrace();
		}
		
			catch (IOException e) {
		e.printStackTrace();	
		
		}
		finally {
			connection.disconnect();
			try {
				reader.close();
			} catch (IOException e2) {
				
			e2.printStackTrace();
			}
		}
		return null;
		
		
	}	//doinbackground method body end
				
		@Override
		protected void onPostExecute(String result) {
			
			super.onPostExecute(result);
					
			tvget.setText(result);
		}
	}//jsontask class body end
	
	}


查看错误,您将在以下行获得 NullPointerException

Looking at the error, you're getting a NullPointerException on the following line:
reader.close();



这意味着读者对象是 null ,这表示在创建阅读器之前,其中一行上会抛出另一个错误。



在尝试调用关闭之前,检查 reader 对象 null 方法:


That means the reader object is null, which suggests you have another error being thrown on one of the lines before the reader is created.

Check the reader object for null before trying to call the close method:

if (reader != null) { reader.close(); }



您将能够看到真正的错误,这将让您诊断并解决问题。


You'll then be able to see the real error, which will let you diagnose and resolve the problem.


这篇关于如何删除JSON解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆