我正在使用Volley Library开发一个android项目。它是一个简单的登录项目。用户必须输入他的手机号码才能看到他/她的详细信息。项目在我的模拟器上正常运行,其API为23.但是当我在较低的API上运行此项目时,会出现主要问题。我的个人手机是Kitkat 4.4.4。该项目没有在我的手机上运行。
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText et_mobile;
Button buttonFind;
String get_result_url = "http://myUrl.php";
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_mobile = (EditText) findViewById(R.id.et_mobile);
buttonFind = (Button) findViewById(R.id.b_find);
builder = new AlertDialog.Builder(MainActivity.this);
buttonFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (et_mobile.getText().toString().trim().length() == 10){
Toast.makeText(MainActivity.this, "yay", Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, get_result_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0); // since only one object is available
String code = jsonObject.getString("code");
if (code.equals("login_failed")){
displayAlertMessage("Mobile Number Miss matched",jsonObject.getString("message"));
}else{
Intent intent = new Intent(MainActivity.this,UserDetails.class);
Bundle bundle = new Bundle();
bundle.putString("user_name",jsonObject.getString("user_name"));
bundle.putString("user_address",jsonObject.getString("user_address"));
bundle.putString("user_mobile",jsonObject.getString("user_mobile"));
bundle.putString("user_notification",jsonObject.getString("user_notification"));
bundle.putString("user_status",jsonObject.getString("user_status"));
intent.putExtras(bundle);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error ...", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}
)
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("user_mobile",et_mobile.getText().toString());
return params;
}
};
MySingleton.getInstance(MainActivity.this).addRequestQueue(stringRequest);
}else {
displayAlertMessage("Error ...","Input Valid Phone Number");
}
}
});
}
public void displayAlertMessage (String title, String msg){
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
这是SingleTon类
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue requestQueue;
private static Context mCtx;
private MySingleton(Context context){
mCtx = context;
requestQueue = getRequestQueue();
}
public RequestQueue getRequestQueue(){
if (requestQueue == null){
requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return requestQueue;
}
// this method will return instance of this class
public static synchronized MySingleton getInstance(Context context){
if (mInstance == null){
mInstance = new MySingleton(context);
}
return mInstance;
}
// this method will add requestQueue
public<T> void addRequestQueue (Request<T> request){
requestQueue.add(request);
}
}
这是我可以看到输出的类
public class UserDetails extends AppCompatActivity {
TextView name, address, mobile, notification, status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_details);
name = (TextView) findViewById(R.id.tv_user_name);
address = (TextView) findViewById(R.id.tv_user_address);
mobile = (TextView) findViewById(R.id.tv_user_mobile);
notification = (TextView) findViewById(R.id.tv_user_notification);
status = (TextView) findViewById(R.id.tv_user_status);
Bundle bundle = getIntent().getExtras();
name.setText(bundle.getString("user_name"));
address.setText(bundle.getString("user_address"));
mobile.setText(bundle.getString("user_mobile"));
notification.setText(bundle.getString("user_notification"));
status.setText(bundle.getString("user_status"));
}
这是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell103.VolleyProject">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UserDetails"></activity>
</application>
</manifest>
这是Build.gradle
defaultConfig {
applicationId "com.example.dell103.DoctorPatientVolleyProject"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
}
这是LogCat中显示的错误
09-07 16:29:39.487 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: com.android.volley.NoConnectionError: java.net.UnknownHostException: http://nirjan_munshi.netne.net/VolleyDemo/json_search_result.php
09-07 16:29:39.487 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:158)
09-07 16:29:39.487 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
09-07 16:29:39.491 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: Caused by: java.net.UnknownHostException: http://nirjan_munshi.netne.net/VolleyDemo/json_search_result.php
09-07 16:29:39.499 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:279)
09-07 16:29:39.499 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
09-07 16:29:39.500 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
09-07 16:29:39.500 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
09-07 16:29:39.500 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
09-07 16:29:39.500 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
09-07 16:29:39.501 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:257)
09-07 16:29:39.501 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
09-07 16:29:39.503 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
09-07 16:29:39.504 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
09-07 16:29:39.505 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/System.err: ... 1 more
09-07 16:30:45.859 20509-20509/com.example.dell103.DoctorPatientVolleyProject W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
答案 0 :(得分:0)
导致您的错误明显
NoConnectionError
UnknownHostException
在您的Android浏览器中尝试http://nirjan_munshi.netne.net/VolleyDemo/json_search_result.php
,如果您获得404
,您的互联网连接就是罪魁祸首! (如果在本地运行时没有查找其他连接问题)