nik666 2016-03-03 13:52 采纳率: 33.3%
浏览 2550

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条回答 默认 最新

  • threenewbee 2016-03-03 13:57
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题