1
我正在使用PHP Web服務創建登錄頁面。但在JSON響應中,我在主JSON外部有一個字符串(check this link)。我試圖用這個link作爲參考來解析它,但我得到這個JSON錯誤。任何人都可以幫助我解析這個JSON的抽象請求嗎?無法將字符串類型的json錯誤值轉換爲jsonobject
public class MainActivity extends AppCompatActivity {
private EditText username, password;
private Button login;
private static final String LOGIN = "http://demo.example.net/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.ET1);
password = (EditText)findViewById(R.id.ET2);
login = (Button)findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = username.getText().toString();
String pass = password.getText().toString();
if(!name.isEmpty() && !pass.isEmpty()){
attemptlogin();
}
}
});
}
private void attemptlogin() {
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
try {
JSONObject jObj = new JSONObject(response);
JSONObject phone = jObj.getJSONObject("testtest123");
String status = phone.getString("success");
// Now check status value
if (status.equals("0")) {
Toast.makeText(getApplicationContext(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
} else if (status.equals("1")) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
// startActivity(new Intent(getApplicationContext(), SecondActivity.class));
// finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("username", username.getText().toString());
params.put("password", password.getText().toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
}
感謝您的回覆和解釋。 'String jsonString = st.substring(index);'?'中的「st」是什麼? –
反正。得到它的工作。非常感謝。 @Ragesh Ramesh。 –
@SomnathPal這裏是什麼**「st」**? –