Я использую eclipse и мой телефон Android с помощью usb для запуска своего приложения. Мой api rest сделан в php и использует xampp, и он запускается на порту 8080 http://localhost:8080. , но я получаю HttpHost Conncetion Exception.Получение исключения HttpHostConnection
protected Void doInBackground(Void... params) {
try {
String username = ed_user.getText().toString();
String address = ed_add.getText().toString();
String age = ed_age.getText().toString();
String contact = ed_cnt.getText().toString();
String email = ed_email.getText().toString();
String gender = ed_gender.getText().toString();
String password = ed_password.getText().toString();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
"http://192.168.56.1:8080/Project/reg.php");
httpRequest.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("username", username);
json.put("address", address);
json.put("contact", contact);
json.put("age", age);
json.put("email", email);
json.put("gender", gender);
json.put("password", password);
Log.i("JSON Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader brReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
strServerResponse = stringBuilder.toString().trim();
Log.e("strServerResponse", ""+strServerResponse.toString());
if (brReader != null) {
strServerResponse = brReader.readLine().toString();
}
if (strServerResponse != null) {
try {
JSONObject jsonObj = new JSONObject(strServerResponse);
String DataStatus = jsonObj.getString("status");
System.out.println(DataStatus);
if (DataStatus.equalsIgnoreCase("SUCCESS")) {
JSONObject mainDataObject = jsonObj
.getJSONObject("data");
String u_name = mainDataObject.optString("u_name");
String u_contact = mainDataObject.optString("u_contact");
String u_address = mainDataObject.optString("u_address");
String u_age = mainDataObject.optString("u_age");
String u_gender = mainDataObject.optString("u_gender");
String u_email = mainDataObject.optString("u_email");
String u_password = mainDataObject.optString("u_email");
SharedPreferences.Editor editor = getSharedPreferences("MyPref", MODE_PRIVATE).edit();
editor.putString("u_name", u_name);
editor.putString("u_contact",u_contact);
editor.putString("u_address", u_address);
editor.putString("u_age", u_age);
editor.putString("u_gender", u_gender);
editor.putString("u_email", u_email);
editor.putString("u_password", u_password);
editor.commit();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
}
finally{
}
} else {
JSONObject mainDataObject = jsonObj
.getJSONObject("error");
String client_id = mainDataObject.optString("error_code");
error_id=Integer.parseInt(client_id);
System.out.print(client_id);
}
} catch (JSONException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
я попытался с помощью моего IP-адрес вместо локального, но до сих пор не работаю
Можете ли вы поместить свой логарифм здесь. –
Вместо 'localhost' используйте свой IP-адрес устройства, например' http: //12.123.123.123: 8080/' – MikeKeepsOnShine