2014-03-13 68 views
0

我可以上傳數據到phpMyAdmin,但不能轉換爲JSON 以下是錯誤org.json.jsonexception值喜不能轉換爲JSONObject的

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; 
}  
} 
+3

PLZ後JSON響應 – KOTIOS

+0

粘貼到這裏結果字符串的值 – Dev

+0

顯示你的JSON響應。 – Piyush

回答

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(); 
0

末回答這個..

1)-put第三try和catch到的AsyncTask的onPostExecute塊2)AsyncTask的第三個參數改爲String。 3)第二次嘗試和catch塊寫 - 返回結果;

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 "); 
      } 
     } 
相關問題