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

时间:2013-11-29 06:03:01

标签: android

这是我的java代码。我想显示登录到另一个页面。 JSOn SJon

public void jsonen()
{
 String result = response.toString();

 try{

 JSONArray jArray = new JSONArray(result);

}
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }



    Log.i("RESULT", result+"");

3 个答案:

答案 0 :(得分:2)

在日志中,你在webservice response中得到JSONObject而不是JSONArray。所以尝试将字符串转换为JSONObject而不是JSONArray:

    JSONObject jsonobj = new JSONObject(result);
     //get value from json object
    teamStatus = jsonobj.getString("teamStatus");

答案 1 :(得分:0)

用于解析和获取数据形成JSON我们:

在src中添加JsonParser.java。而java文件将是这样的:

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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

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

    // constructor
    public JSONParser() {

    }
    public JSONObject getJSONFromUrl(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, "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();
            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;

    } 
}

现在在src中添加UserFunction.Java文件。它会是这样的:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

public class UserFunctions {

    private JSONParser jsonParser;

    private static String loginURL = YOUR_URL;

public UserFunctions() {
        jsonParser = new JSONParser();
    }
public JSONObject loginUser(String email, String password) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
}

你的login.java将是:

String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
//do your staff
}catch{
}

答案 2 :(得分:0)

我有同样的问题并修复它。检查你的php文件,特别是在转换JSON之前打印(&#34;&#34;)。