2 nik666 nik666 于 2016.03.03 21:52 提问

Value  of type java.lang.String cannot be

public class Register_ extends Activity{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText name;
EditText password;
Button login_button;

private static String url = "http://192.168.1.203/EnjoyDemo/test.php";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
    name=(EditText)findViewById(R.id.editText1);
    password=(EditText)findViewById(R.id.editText2);
    login_button=(Button)findViewById(R.id.button1);
    login_button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            if(validate()){                
            new Up().execute();            
        }
            }
    });
}
private boolean validate()
{
    String name1 = name.getText().toString().trim();
    if (name1.equals(""))
    {
        DialogUtil.showDialog(this, "����û����д����", false);
        return false;
    }
    String password1 = password.getText().toString().trim();
    if (password1.equals(""))
    {
        DialogUtil.showDialog(this, "����û����д����", false);
        return false;
    }

    return true;
}
class Up extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Register_.this);
        pDialog.setMessage("�����ϴ�..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    protected String doInBackground(String... args) {
        String name1 = name.getText().toString();
        String password1 = password.getText().toString();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name1));
        params.add(new BasicNameValuePair("password", password1));
       try{
        JSONObject json = jsonParser.makeHttpRequest(url,
                "POST", params);


        String message = json.getString(TAG_MESSAGE);
        return message;
       }catch(Exception e){
           e.printStackTrace(); 
           return "";          
       }
    }

}

}

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 
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {    
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
            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"));
        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());
        Log.d("json", json.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;

}

}
做注册界面传输json字符串时出现了这个错误 菜鸟初学 没有指导不知道该怎么做了。。。有没有人指导下怎么改 谢谢了 只有这个错误显示

org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

4个回答

caozhy
caozhy   Ds   Rxr 2016.03.03 21:57

检查特殊字符的转义,中文的编码。你这个错误是说,你的字符串没法解析成json

nik666
nik666 我查了有说是返回的json带有bom头 但是简略说说 没有介绍解决的方法 请问你知道吗
接近 2 年之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.04 09:10

中文转码失败了,你的json有bom头吧?

lxk_1993
lxk_1993   Rxr 2016.03.04 11:03

可以试试自己截取 返回的json数据

acarida
acarida   2016.03.04 16:16

应该是BOM header的原因。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!