0
Я получаю эту ошибку, когда пытаюсь нажать кнопку.Попытка отправить запросы JSON, получить java.lang.IllegalStateException: целевой хост не должен быть нулевым или задан в параметрах
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=172.27.123.71
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at applicatoin.MainActivity$1.onClick(MainActivity.java:79)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.view.View.performClick(View.java:4438)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-05 22:12:30.797 31048-31048/applicatoin W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
Я пытаюсь отправить запрос на отправку JSON на сервер. Вот мой код:
package applicatoin;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
public class MainActivity extends Activity {
Button sendIPbutton; //Button for sending IP Address
EditText mEdit; //Get info from what user enters in form
//TextView mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*get reference to views*/
sendIPbutton = (Button) findViewById(R.id.sendIP);
mEdit = (EditText) findViewById(R.id.enterIP);
/*add click listener to Button "sendIPbutton"*/
sendIPbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Add JSON Logic here
String ip = (mEdit).getText().toString();
String json = "{\"lights\": [{\"lightId\": 1, \"red\":242,\"green\":116,\"blue\":12, \"intensity\": 0.5}],\"propagate\": true}";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(ip);
try {
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "ms3/json");
httpPost.setHeader("Content-type", "ms3/json");
HttpResponse response = httpClient.execute(httpPost, httpContext);
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity);
} catch (Exception e){
e.printStackTrace();
}
mEdit = (EditText)findViewById(R.id.enterIP); //get text from form?
}
});
// addListenerOnButton(); //Added for button to Send IP Address
}
public void sendJSONTest(View view){
switch (view.getId()){
case R.id.sendIP:
String ip = (mEdit).getText().toString();
String json = "{\"lights\": [{\"lightId\": 1, \"red\":242,\"green\":116,\"blue\":12, \"intensity\": 0.5}],\"propagate\": true}";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(ip);
try {
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "ms3/json");
httpPost.setHeader("Content-type", "ms3/json");
HttpResponse response = httpClient.execute(httpPost, httpContext);
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity);
} catch (Exception e){
e.printStackTrace();
}
break;
}
}
public void sendJSONTest(String ip) {
String json = "{\"lights\": [{\"lightId\": 1, \"red\":242,\"green\":116,\"blue\":12, \"intensity\": 0.5}],\"propagate\": true}";
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(ip);
try {
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "ms3/json");
httpPost.setHeader("Content-type", "ms3/json");
HttpResponse response = httpClient.execute(httpPost, httpContext);
HttpEntity entity = response.getEntity();
String jsonString = EntityUtils.toString(entity);
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Я не уверен, что причина ошибки. Кнопка идентифицирует текст в текстовом поле, но когда мы пытаемся выполнить HTTPrequest, мы получаем системную ошибку. JSON находится в правильном формате. Сервер даже не получает запрос.
похоже, что google.com не работает – Selvin
1) Не делайте сетевых запросов в потоке пользовательского интерфейса. 2) На самом деле не делайте сетевых запросов в потоке пользовательского интерфейса;). 3) Обычно для JSON Content-Type должен быть application/json. 4) Можете ли вы выполнить ping-сервер на указанном IP-адресе? –