On StringRequest,Response.ErrorListener()giver null error。
在日志中显示:
E / Login_1_Phone:发布参数:{mobile = 8681070970}
E / Volley:[7239] BasicNetwork.performRequest:http://54.254.177.58/android_sms/msg91/request_sms.php的意外回复代码404
E / Login_1_Phone:错误:null
源代码:
活动请求:
package com.iotaconcepts.dr;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
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.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.iotaconcepts.dr.R;
import com.iotaconcepts.dr.app.Config;
import com.iotaconcepts.dr.app.MyApplication;
import com.iotaconcepts.dr.Utils.SharedPreference;
import com.iotaconcepts.dr.service.HttpService;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* Created by prasang on 29/1/16.
*/
public class Login_1_Phone extends Activity{
Button ok;
EditText et_phone;
String phoneNumber;
private static String TAG = Login_1_Phone.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_1_phone);
init();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phoneNumber = et_phone.getText().toString();
if (phoneNumber.length() == 0) {
Toast.makeText(Login_1_Phone.this, "Please enter mobile number", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Login_1_Phone.this, Login_2_OTP.class);
startActivity(i);
}
else if (phoneNumber.length() != 10 ) {
Toast.makeText(Login_1_Phone.this, "Please enter a valid mobile number", Toast.LENGTH_SHORT).show();
}
else if (phoneNumber.length() == 10) {
//TODO: API Here
requestForSMS(phoneNumber);
}
}
});
}
private void init()
{
et_phone = (EditText)findViewById(R.id.et_login_phone_number);
ok = (Button)findViewById(R.id.bt_login_phone_ok);
}
private void requestForSMS (final String mobile) {
StringRequest strReq = new StringRequest(Request.Method.POST,
Config.URL_REQUEST_SMS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
try {
JSONObject responseObj = new JSONObject(response);
// Parsing json object response
// response will be a json object
boolean error = responseObj.getBoolean("error");
String message = responseObj.getString("message");
// checking for error, if not error SMS is initiated
// device should receive it shortly
if (!error) {
Toast.makeText(getApplicationContext(), "Message: " + message, Toast.LENGTH_SHORT).show();
Intent i = new Intent(Login_1_Phone.this, Login_2_OTP.class);
startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "Error: " + message, Toast.LENGTH_LONG).show();
}
// hiding the progress bar
//progressBar.setVisibility(View.GONE);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
//progressBar.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "onErrorResponse: " + error.getMessage(), Toast.LENGTH_SHORT).show();
//progressBar.setVisibility(View.GONE);
}
}) {
/**
* Passing user parameters to our server
* @return
*/
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("mobile", mobile);
Log.e(TAG, "Posting params: " + params.toString());
return params;
}
};
strReq.setShouldCache(false);
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq);
}
}
HttpService类
package com.iotaconcepts.dr.service;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import com.iotaconcepts.dr.MainActivity;
import com.iotaconcepts.dr.app.Config;
import com.iotaconcepts.dr.app.MyApplication;
import com.iotaconcepts.dr.Utils.SharedPreference;
public class HttpService extends IntentService {
private static String TAG = HttpService.class.getSimpleName();
public HttpService() {
super(HttpService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
String otp = intent.getStringExtra("otp");
verifyOtp(otp);
}
}
/**
* Posting the OTP to server and activating the user
* @param otp otp received in the SMS
*/
private void verifyOtp(final String otp) {
StringRequest strReq = new StringRequest(Request.Method.POST, Config.URL_VERIFY_OTP, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject responseObj = new JSONObject(response);
// Parsing json object response
// response will be a json object
boolean error = responseObj.getBoolean("error");
String message = responseObj.getString("message");
if (!error) {
// parsing the user profile information
JSONObject profileObj = responseObj.getJSONObject("profile");
String name = profileObj.getString("name");
String email = profileObj.getString("email");
String mobile = profileObj.getString("mobile");
//PrefManager pref = new PrefManager(getApplicationContext());
//pref.createLogin(name, email, mobile);
// User verified! Go to main activity now.
Intent intent = new Intent(HttpService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("otp", otp);
Log.e(TAG, "Posting params: " + params.toString());
return params;
}
};
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq);
}
}