2015-12-08 15:44

Android JSON解析返回null

I'm Trying to connect MYSQL with android application using php and JSON files, the problem I'm facing in the moment is that JSONParser Class always return null, and I don't know why since I can view my JSON file and it's valid

and I'm also I have two exceptions in my code the first one is:

        12-08 09:17:43.486 29666-29666/alharbi.atheer.gym_managment_system E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
        12-08 09:17:43.498 29666-29666/alharbi.astrong texttheer.gym_managment_system E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
        12-08 09:17:43.518 29666-29666/alharbi.atheer.gym_managment_system E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
        12-08 09:17:43.522 29666-29666/alharbi.atheer.gym_managment_system E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384

The Second one is :

12-08 09:17:46.526 29666-29740/alharbi.atheer.gym_managment_system E/Buffer Error: Error converting result java.lang.NullPointerException: lock == null
12-08 09:17:46.526 29666-29740/alharbi.atheer.gym_managment_system E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of 

this is the class that contain the problem

public class ViewInfo extends AppCompatActivity {
    JSONParser jsonParser;
    ProgressDialog progressDialog;
    int value;
    String[] names,emails,levels,ids, weights,hieghts,genders,bds,addresses,ages,phones;
    ListView listView2;
    protected void onCreate(Bundle savedInstanceState) {
        jsonParser=new JSONParser();

        new DisplayViewInfo().execute();

    class DisplayViewInfo extends AsyncTask<String,String,String> {

        protected void onPreExecute() {
            progressDialog=new ProgressDialog(ViewInfo.this);

        protected String doInBackground(String... strings) {

          List<NameValuePair> list= new ArrayList<NameValuePair>();
          //  HashMap<String,String> list = new HashMap<>();

            // jsonObject=jsonParser.makeHttpRequest("","POST",list);
           JSONObject jsonObject=jsonParser.makeHttpRequest("", "POST", list);

                if(jsonObject!=null && !jsonObject.isNull("value")){

                    JSONArray jsonArray=jsonObject.getJSONArray("Customers");
                    names=new String[jsonArray.length()];
                    emails=new String[jsonArray.length()];
                    phones=new String[jsonArray.length()];
                    ids=new String[jsonArray.length()];
                    weights=new String[jsonArray.length()];
                    hieghts=new String[jsonArray.length()];
                    genders=new String[jsonArray.length()];
                    levels=new String[jsonArray.length()];
                   ages=new String[jsonArray.length()];
                    addresses=new String[jsonArray.length()];
                    bds=new String[jsonArray.length()];
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject objcet=jsonArray.getJSONObject(i);


            }catch (Exception e){
                Log.d("ERROR", e.getMessage());

            return null;
        protected void onPostExecute(String s) {
                Toast.makeText(getApplicationContext(), "Done...", Toast.LENGTH_LONG).show();
                ArrayAdapter<String> adapter=new ArrayAdapter<String>(ViewInfo.this,android.R.layout.simple_list_item_1,,names);
            }else if(value==0)
            else Toast.makeText(getApplicationContext(),"OUT...",Toast.LENGTH_LONG).show();




This is My JSON Parser :

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {


// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method.equals("GET")){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
        //  e.printStackTrace();
    } catch (IOException e) {

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());

    // return JSON String
    return jObj;



Can anyone please help me? I've been straggling for days because of these errors

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douqiao8032 douqiao8032 6年前

    From the look of that JSON error, it seems like you are not getting a valid response, and your response String is just empty. When this happens, I usually check both the html response code of my web service call, as well as the actual String after I build it. This helps identify whether the web service is the problem, or if your call to the web service is the problem.

    Another thing I should mention is that you are using deprecated code for your web calls. The apache http code was removed from the Android framework, and so you should be using HttpURLConnection to make web service calls instead.

    I'm in a pretty good mood today, and I'm feeling helpful, so I'm just going to give you the code you should be using to get this JSON response :)

    //The JSON we will get back as a response from the server
    JSONObject jsonResponse = null;
    //Http connections and data streams
    URL url;
    HttpURLConnection httpURLConnection = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
        //open connection to the server
        url = new URL("your_url_to_web_service");
        httpURLConnection = (HttpURLConnection) url.openConnection();
        //set request properties
        httpURLConnection.setDoOutput(true); //defaults request method to POST
        httpURLConnection.setDoInput(true);  //allow input to this HttpURLConnection
        httpURLConnection.setRequestProperty("Content-Type", "application/json"); //header params
        httpURLConnection.setRequestProperty("Accept", "application/json"); //header params
        httpURLConnection.setFixedLengthStreamingMode(jsonToSend.toString().getBytes().length); //header param "content-length"
        //open output stream and POST our JSON data to server
        outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream());
        outputStreamWriter.flush(); //flush the stream when we're finished writing to make sure all bytes get to their destination
        //prepare input buffer and get the http response from server
        StringBuilder stringBuilder = new StringBuilder();
        int responseCode = httpURLConnection.getResponseCode();
        //Check to make sure we got a valid status response from the server,
        //then get the server JSON response if we did.
        if(responseCode == HttpURLConnection.HTTP_OK) {
            //read in each line of the response to the input buffer
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
            bufferedReader.close(); //close out the input stream
        try {
            //Copy the JSON response to a local JSONObject
            jsonResponse = new JSONObject(stringBuilder.toString());
        } catch (JSONException je) {
    } catch (IOException ioe) {
    } finally {
        if(httpURLConnection != null) {
            httpURLConnection.disconnect(); //close out our http connection
        if(outputStreamWriter != null) {
            try {
                outputStreamWriter.close(); //close our output stream
            } catch (IOException ioe) {
    //Return the JSON response from the server.
    return jsonResponse;
    点赞 评论 复制链接分享