我使用的服务是获取字符串值,响应是json对象。当我在输入值后单击按钮时,出现错误com.android.volley.server
错误。我检查了邮递员的服务器,它工作正常。
public class MainActivity extends AppCompatActivity {
// json object response url
private String urlJsonObj = "http://walletuncle.com/myservice.asmx/UserLogin/";
// json array response url
private String urlJsonArry = "http://api.androidhive.info/volley/person_array.json";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeObjectRequest, btnMakeArrayRequest;
// Progress dialog
private ProgressDialog pDialog;
private TextView txtResponse;
private EditText editusername;
private EditText editpassword;
public static final String KEY_USERNAME = "UserId";
public static final String KEY_PASSWORD = "Password";
private String jsonresponse;
// temporary string to show the parsed response
private String jsonResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
// btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
editusername = (EditText) findViewById(R.id.editusername);
editpassword = (EditText) findViewById(R.id.editpassword);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json object request
makeJsonObjectRequest();
}
});
}
private void makeJsonObjectRequest() {
final String username = editusername.getText().toString().trim();
final String password = editpassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, urlJsonObj,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Data added", "Data done");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_USERNAME, username);
params.put(KEY_PASSWORD, password);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest);
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object // Parsing json object response
// response will be a json object
String name = response.getString("Name");
//String email = response.getString("email");
//JSONObject phone = response.getJSONObject("phone");
//String home = phone.getString("home");
//String mobile = phone.getString("mobile");
jsonResponse = "";
jsonResponse += "Name: " + name + "\n\n";
//jsonResponse += "Email: " + email + "\n\n";
//jsonResponse += "Home: " + home + "\n\n";
//jsonResponse += "Mobile: " + mobile + "\n\n";
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
//Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}