java.lang.String类型的org.json.jsonexception值hi无法转换为JSONObject

时间:2014-03-13 08:29:33

标签: android

我可以将数据上传到phpMyAdmin,但无法转换为JSON 这是错误

org.json.jsonexception value hi of type java.lang.String cannot be converted to JSONObject



public class MainActivity extends Activity {

String name;
String id;
InputStream is=null;
String result=null;
String line=null;
int code;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    final EditText e_id=(EditText) findViewById(R.id.editText1);
    final EditText e_name=(EditText) findViewById(R.id.editText2);
    Button insert=(Button) findViewById(R.id.button1);

    insert.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        id = e_id.getText().toString();
        name = e_name.getText().toString();

        insert();
    }
});
}

public void insert()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));

    try
    {
    HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("192.168.1.2/testphp2.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
}
    catch(Exception e)
{
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
        Toast.LENGTH_LONG).show();
}     

    try
    {
        BufferedReader reader = new BufferedReader
        (new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
    {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    Log.e("pass 2", "connection success ");
}
    catch(Exception e)
{
        Log.e("Fail 2", e.toString());
}     

try
{
        JSONObject json_data = new JSONObject(result);
        code=(json_data.getInt("code"));

        if(code==1)
        {
    Toast.makeText(getBaseContext(), "Inserted Successfully",
        Toast.LENGTH_SHORT).show();
        }
        else
        {
     Toast.makeText(getBaseContext(), "Sorry, Try Again",
        Toast.LENGTH_LONG).show();
        }
}
catch(Exception e)
{
        Log.e("Fail 3", e.toString());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}    
}

2 个答案:

答案 0 :(得分:0)

使用AsyncTask并在OnPostExecute中编写此代码

    JSONObject json_data = new JSONObject(result);
    code=(json_data.getInt("code"));

    if(code==1)
    {
Toast.makeText(getBaseContext(), "Inserted Successfully",
    Toast.LENGTH_SHORT).show();
    }
    else
    {
 Toast.makeText(getBaseContext(), "Sorry, Try Again",
    Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

迟到回答这个问题。

1) - 输入第3次尝试并捕获AsyncTask的onPostExecute块 2)AsyncTask的第三个参数更改为String。 3)第二次尝试并捕获块写入 - 返回结果;

 try {
                BufferedReader reader = new BufferedReader
                        (new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line );
                }
                is.close();
                result = sb.toString().substring(0, sb.toString().length()-1);
                Log.d("pass 2", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 2", e.toString());
                Log.d(" fail 2 "," exception pass 2 ");
            }
            return result;

        }

        @Override
        protected void onPostExecute(String result) {

            try {

                Log.e("pass 3", "connection success "+ result);
            } catch (Exception e) {
                Log.e("Fail 3", e.toString());
                Log.d(" fail 3 "," exception pass 3 ");
            }
        }