【发布时间】:2016-03-03 06:35:42
【问题描述】:
我想调用在服务器上编写的 PHP 脚本的 url,在比较用户名密码后返回错误或成功,但是当我点击提交按钮时,它给了我这个错误
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);
?>
【问题讨论】:
-
不完全确定,但看起来 php 脚本只响应了字符串
Error。 -
这个错误发生在哪一行
-
我通过 json 对象发送用户名和密码。在 php 中,它检查用户名和密码,但它返回以下错误行:com.pace.testformwithonline E/Response error: com.android.volley.ParseError: org.json.JSONException: Value Error of type java.lang.String cannot转换为 JSONObject。
-
您能在此处发布预期的 json 响应吗?我认为您在字符串中存储了一个 json 对象值。
-
这是我的 Logcat:03-03 11:49:32.092 2343-2343/com.pace.testformwithonline E/响应错误:com.android.volley.ParseError: org.json.JSONException: Value java.lang.String 类型的错误无法转换为 JSONObject
标签: php android android-volley jsonresponse jsonobjectrequest