2016-09-07 60 views
0

我正在開發一個使用Volley庫的Android項目。它是一個簡單的登錄項目。用戶必須輸入他的手機號碼才能查看他/她的詳細信息。該項目運行良好,我的模擬器的API是23.但主要問題發生在我運行這個項目在較低的API。我的個人手機是Kitkat 4.4.4。該項目沒有在我的手機上運行。如何在較低API中使用Volley庫

這是我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(); 
} 
} 

這是單例類

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 
+1

包含在真實設備上運行時出現的錯誤。 – ishmaelMakitla

+0

還分享您的應用程序的清單和gradle文件。 –

+0

我把所有東西都撕成了碎片 –

回答

0

你呃ROR是UnknownHostException

明確NoConnectionError造成Android瀏覽器嘗試http://nirjan_munshi.netne.net/VolleyDemo/json_search_result.php,如果你得到一個404你的互聯網連接是罪魁禍首! (如果您在本地運行時不查找其他連接問題)

+0

沒有在我的手機瀏覽器上正常工作。有人告訴我,它在我的另一個棒棒糖手機上工作。但它不適用於kitkat。這個問題已經陷入了一個多星期。而且我沒有在本地運行。你可以看到免費的域名 –

相關問題