我有一個LG G3,當我運行我的應用程序時,排氣失敗。我所做的只是發送一個post請求到我的本地服務器,它在模擬器上完美工作,但在設備上失敗。我得到的錯誤是超時錯誤。這裏是代碼:排氣在模擬器上運行,但在設備上失敗
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
final TextView textview = (TextView) findViewById((R.id.test));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequest("example", "0", "0", "0", "0", "0", "0");
}
});
}
public void sendRequest(final String location, final String mood, final String gender, final String age,
final String day, final String month, final String year) {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "http://10.0.2.2:8000/polls/add/";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, "works", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error is: " + error.toString(),
Toast.LENGTH_LONG).show();
Log.e("VOLLEY", error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parameters = new HashMap<>();
parameters.put("age", age);
parameters.put("location", location);
parameters.put("mood", mood);
parameters.put("gender", gender);
parameters.put("day", day);
parameters.put("month", month);
parameters.put("year", year);
return parameters;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(stringRequest);
}
}
任何幫助將非常感激。謝謝!
凌空不提供本地接入 – user6313669
的可能的複製[訪問來自USB PC的本地主機連接的Android移動設備(HTTP://計算器的.com /問題/ 9887621 /訪問-本地主機 - 的-PC-從-USB連接-機器人的移動 - 疊維ce) – Bryan
請參閱http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device – user6313669