我尝试按下按钮时出现此错误。
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的格式正确。服务器甚至没有收到请求。
答案 0 :(得分:0)
Use Seprate classes For sending all json request i will show one single demo
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
new SendRequest().execute();
mEdit = (EditText)findViewById(R.id.enterIP); //get text from form?
}
});
// addListenerOnButton(); //Added for button to Send IP Address
}
}
Class SendRequest extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
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();
}
}
}