用于seng Json请求的Android Estabilish Connection错误

时间:2014-10-02 08:45:58

标签: android json android-json

我使用Json Object和Web上的示例将简单数据发送到服务器的简单代码。加载requet URL后,我获得了成功的结果。但我无法从andrid应用程序发送任何请求,我收到此错误:Cannot Estabilish Connection

我在网上的结果:

["Hello"]

我的简单代码:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sendJson("tsms_mahdi","123456");
    }

    protected void sendJson(final String username, final String password) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost("site.com/example.php");  /* -> http://www.example.com/example.php*/
                    json.put("username", username);
                    json.put("password", password);
                    StringEntity se = new StringEntity( json.toString());
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);
                    /*Checking response */
                    if(response!=null){
                        //Get the data in the entity
                        InputStream in = response.getEntity().getContent();
                        Log.e("Content", "in");
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    Log.e("Error", "Cannot Estabilish Connection");
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();
    }
}

我的代码问题是什么?我在这一行得到错误:

response = client.execute(post);

更新:

Error﹕ java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=example.com/example.php

1 个答案:

答案 0 :(得分:0)

尝试使用我的代码并替换您的值

HttpClient client = new DefaultHttpClient();

            String message;
            HttpPost p = new HttpPost("http://www.example.com/example.php");
            JSONObject object = new JSONObject();
            try {

                object.put("username", username);
                object.put("password", password);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            try {
                message = object.toString();
                p.setEntity(new StringEntity(message, "UTF8"));
                p.setHeader("Content-type", "application/json");
                HttpResponse resp = httpClient.execute(p);
                if (resp != null) {
                    String responseBody = EntityUtils.toString(resp.getEntity());
                    if (responseBody.length() > 0) {
                             jsonResponse = new JSONObject(responseBody);
                        }
            } catch (Exception e) {
                e.printStackTrace();
            }

和jsonResponse是来自webservice的响应