将JSON数组发布到PHP

时间:2014-02-10 00:54:31

标签: php android arrays json

我试图将JSONArray发送到我的服务器,我的PHP脚本将获取值并搜索我的数据库以查找匹配项,但我无法将其发送。我一直在JSONException

  

"解析数据时出错org.json.JSONException:类型的值   java.lang.String无法转换为JSONObject"

如果我在服务器端执行vardump,它会显示为null。

我有一个如下所示的数组:

ArrayList myList:
  ["BoUsQJP", "ldT6brS", "iVRuJvmP"]

我使用以下代码创建对象并发送到我的JSON解析器。我正在使用"标记"为php脚本服务器端处理它。

public JSONObject checkInList(ArrayList<String> myList) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", checkinlist_tag));
    params.add(new BasicNameValuePair("refids", myList.toString()));
    JSONObject json = jsonParser.getJSONCheckList(checkinlistURL, params);
    System.out.println("Show me....." + params);
    return json;
}

JSON解析器代码:

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

    // Making HTTP request
    try {
        // 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();

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

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", 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);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

我错过了什么?

2 个答案:

答案 0 :(得分:0)

首先关闭错误,你没有JSONObject,你有一个JSONArray。数组中的类型是String。所以你需要将它解析为json字符串数组。或者你可以让他串起来,修剪开口和关闭括号“[]”,然后修剪空白区域。现在根据逗号分开。

好的,我会推荐这样的东西

@Override
    protected List<T> doInBackground(Void... params)
    {
        try
        {
            if (httpClient == null)
                httpClient = new DefaultHttpClient();

            HttpResponse response = httpClient.execute(request);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK)
            {
                Log.d(TAG, "FAILED STATUS");
                Log.d(TAG, "reason: " + response.getStatusLine().getReasonPhrase() + ", code: " + response.getStatusLine().getStatusCode());
                Log.d(TAG, "attempted url: " + request.getURI().toString());
                return null;
            }
            InputStream contentInputStream = response.getEntity().getContent();
            String data = IOUtils.toString(contentInputStream);
            Log.d(TAG, "array json = " + data);
            data = data.toString();
            List<T> listOfT = Json.getGson().fromJson(data, type);
            return listOfT;

        } catch (ClientProtocolException e)
        {
            Log.d("TAG", "ClientProtocolException", e);
        } catch (IOException e)
        {
            Log.d("TAG", "IOException", e);
        }
        return null;
    }

因为您正在寻找字符串列表,所以您可以将json行更改为此

List<String> list = Json.getGson().fromJson(data, new TypeToken<List<String>>(){}.getType());

你需要Gson jar,我认为是IOUtils的apache jar。

如果您不想使用Gson,请不要担心,您需要的所有内容都应该在字符串data中。

如果你愿意,也可以忽略泛型,这只是我为自己制作的一个大型多功能易用库的子片,因为我做了很多应用程序。此外,如果您想接受更多状态'然后只需200 OK,请将它们包含在if条件中。

另一件事,这样做很容易

JSONObject jObj = new JSONObject();
jObj.put("key", "value");

使用上面的代码也很容易创建像这样的JSONArray或JSONObject

JSONArray jArray = new JSONArray(data);
JSONObject jObj = new JSONObject(data);

答案 1 :(得分:0)

通常,PHP脚本中必定存在一些错误。 你检查了你的日志吗?有什么不寻常的?如果我猜对了。 json必定是不对的。

   Log.e("JSON", json);