2016-03-03 63 views
0

我想打電話給的網址,讓PHP腳本寫在服務器返回我的錯誤或比較的用戶名密碼後的成功,但是當我在提交按鈕點擊它給了我這個錯誤Android JSONObject請求方法發生錯誤的POST。 「不能轉換爲JSONObject的」

com.pace.testformwithonline E/Response error: 
com.android.volley.ParseError: org.json.JSONException: Value Error of type 
java.lang.String cannot be converted to JSONObject 

包com.pace.testformwithonline;

import android.app.Activity; 
import android.app.DownloadManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.volley.Request; 
import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONException; 
import org.json.JSONObject; 

public class MainActivity extends Activity { 
    private EditText username; 
    private EditText password; 
    private Button btnSubmit; 
    private TextView result; 
    private static final String URL="http://pace-tech.com/nomi/zaigham/android.php"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     username=(EditText)findViewById(R.id.editText_username); 
     password= (EditText) findViewById(R.id.editText_password); 
     btnSubmit=(Button)findViewById(R.id.btn_submit); 
     result=(TextView)findViewById(R.id.text_result); 
     btnSubmit.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       result.setText(""); 
      postJsonobjectRequest(URL); 
      } 

     }); 

    } 
    public void postJsonobjectRequest(String url){ 
     JSONObject params=new JSONObject(); 
     RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
     try { 
      params.put("username", username.getText().toString()); 
      params.put("password", password.getText().toString()); 
     } 
     catch (JSONException e){ 
     e.printStackTrace(); 
     } 
     JsonObjectRequest jsOBJRequest=new JsonObjectRequest 
       (Request.Method.POST, URL, params, new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       // Log.e("Response is",response.toString()); 
       try { 
        Log.e("Response is",response.toString()); 
        String Success = response.getString("Success").toString(); 
        String Message = response.getString("Message").toString(); 
        if(Success=="0") { 
         result.setText(Message); 
        } 
        else { 
         Toast.makeText(getApplicationContext(),Message,Toast.LENGTH_LONG); 
        } 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

       Log.e("Response error",error.toString()); 
       result.setText("Error Is :"+ error.toString()); 
      } 
     }); 
     queue.add(jsOBJRequest); 
    } 
} 

這裏是PHP腳本

<?php 
$username = "zaigi"; 
$password = "1111"; 

$androidUserName = json_decode($_POST['username']); 
$androidPassword = json_decode($_POST['password']); 

if($androidUserName == $username && $androidPassword == $password){ 
$response = "Correct"; 
}else{ 
$response = "Error"; 
} 
echo json_encode($response); 
?> 
+0

不能完全確定,但它看起來像PHP腳本與剛剛字符串'Error'迴應。 – VolkerK

+0

發生此錯誤,在什麼行 –

+0

發佈您的logcat。 –

回答

0

這是分析錯誤,這意味着凌空無法解析,從你的服務器的到來,所以查收的響應串的響應,是它在適當的JSON格式與否?你可以檢查你的響應字符串here是否合適的JSON。

0

我知道這個問題。它發生在我身上一次。 使用StringRequest而不是JsonObjectRequest。 這是可怕的錯誤。 params不通過JsonObjectRequest。

請參見更多 Volley JsonObjectRequest Post request not working

你會得到響應,字符串,則其轉換成JSONObject的這個環節。

+0

有太多的錯誤我無法解決 –

+0

這是正確的! Android代碼有錯誤! JsonObjectRequest有一些無法發佈參數的bug。 –

+0

但你仍然可以弄清楚如何創建StringRequest。 PARAMS問題是排球的錯誤 打開鏈接的答案 –

0

在PHP腳本做一些這樣的事..

$data = file_get_contents('php://input'); 
    $json = json_decode($data); 
    //username and password sent from android 
    $username=$json->{'username'}; 
    $password=$json->{'password'}; 


    $androidUserName = $username; 
    $androidPassword = $password; 

    if($androidUserName == $myusername && $androidPassword == $mypassword){ 
     $response = array('result' => "Correct"); 
    }else{ 
     $response = array('result' => $androidUserName,'password' => $androidPassword); 
    } 
    header('Content-type: application/json'); 
    echo json_encode($response); 
相關問題