0

我搜索了许多解决方案,但仍然不起作用。我仍然收到将字符串转换为的错误JSONObject 这是我的代码:

 public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

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

            }else if(method == "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) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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 + "\n");
            }
            is.close();
            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", json);
            //Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

这是我要解析的字符串:

{"products":[{"pid":"1","name":"abc","price":"222.00","created_at":"2013-04-02 12:44:26","updated_at":"0000-00-00 00:00:00"}],"success":1}

从这里学到了这段代码

也不jObj = new JSONObject(json.substring(3));适合我。

我解决了问题:问题出在我的字符串上,我实际上在我的 php 中回显了一些评论,它包含在我的 json 字符串中。所以我从我的 php 中删除了我的回声,一切都很顺利。谢谢!

4

3 回答 3

0

JSON的无效。您缺少一个左括号[,表明JSONArray它应该是:

    {
        "products": [
          {
          "pid":"1",
          "name":"abc",
          "price":"222.00",
          "created_at":"2013-04-02 12:44:26",
          "updated_at":"0000-00-00 00:00:00"
          }
        ],
        "success":1
    }

更新:

你的问题是这样的:

if (method == "POST") { .. } // and same for GET

这总是返回false,因为您正在尝试将字符串与==比较引用的运算符进行比较。您必须使用equals()方法,因为您想比较字符串的值。

于 2013-04-02T07:17:21.573 回答
0
package com.example.aam1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import org.json.JSONObject;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;

    static JSONArray jarr = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

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

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

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

            }else if(method == "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();

                if(httpEntity == null)
                {      String msg = "No response from server";   
                return null;
                }            
                is = httpEntity.getContent();
            }           

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

        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 + "\n");
            }
            is.close();
            json = sb.toString().trim();
            Log.i("StringBuilder...", json);
        } 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.substring(3));
            jarr = new JSONArray(json);
        } catch (JSONException e) {
           // Log.e("JSON Parser", "Error parsing data " + e.toString());
            Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);

            try {
                jObj = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
            } catch (Exception e0) {

                Log.e("JSON Parser0", "Error parsing data0 [" + e0.getMessage()+"] "+json);
                Log.e("JSON Parser0", "Error parsing data0 " + e0.toString());

                try {
                    jObj = new JSONObject(json.substring(1));
                } catch (Exception e1) {

                    Log.e("JSON Parser1", "Error parsing data1 [" + e1.getMessage()+"] "+json);
                    Log.e("JSON Parser1", "Error parsing data1 " + e1.toString());

                    try {
                        jObj = new JSONObject(json.substring(2));
                    } catch (Exception e2) {

                        Log.e("JSON Parser2", "Error parsing data2 [" + e2.getMessage()+"] "+json);
                        Log.e("JSON Parser2", "Error parsing data2 " + e2.toString());

                        try {
                            jObj = new JSONObject(json.substring(3));
                        } catch (Exception e3) {

                            Log.e("JSON Parser3", "Error parsing data3 [" + e3.getMessage()+"] "+json);
                            Log.e("JSON Parser3", "Error parsing data3 " + e3.toString());

                        }
                    }
                }
            }

        }

        // return JSON String
        return jObj;
 //       return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));

    }
} 
于 2013-10-04T05:13:21.550 回答
0

确保验证您的 json。您可以使用http://jsonlint.com/

于 2013-04-02T08:03:39.030 回答