发布数据时无法获得响应?

时间:2013-11-13 07:00:48

标签: java android web-services http httprequest

我在点击按钮输入方法时将一些数据发布到url。 但log cat显示host = null

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        }


public void entry(View v)
{
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("www.uthsms.net/index.php?on=sms");


    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("country", "91"));
    nameValuePairs.add(new BasicNameValuePair("pakistan", "91"));
    nameValuePairs.add(new BasicNameValuePair("sindh", "8903466567"));
    nameValuePairs.add(new BasicNameValuePair("gateway", "0"));
    nameValuePairs.add(new BasicNameValuePair("hyderabad", "12345"));
    nameValuePairs.add(new BasicNameValuePair("remLen", "135"));
    nameValuePairs.add(new BasicNameValuePair("recaptcha_challenge_field", "03AHJ_VuvO-0yifgUvXaYPdlTF5QBZwKQlo-gQE8FYioRNi3klTtM_MLDnbzuOH8uUFzX1LXVltrZy8kBcTDY176rLVu3M1EvbkF3fQKxQ3t3nBedlshOvNXHZDFCO5I4Sdm4AkIdpQViTRBDzB6pU9OIombEKY6yy7OsY1Faob6Z47pNAaKWqFGc"));
    nameValuePairs.add(new BasicNameValuePair("recaptcha_response_field", "2485842 76"));
    nameValuePairs.add(new BasicNameValuePair("x", "126"));
    nameValuePairs.add(new BasicNameValuePair("y", "31"));
    nameValuePairs.add(new BasicNameValuePair("button", "Send SMS"));
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    // Execute HTTP Post Request
    try {
        HttpResponse response = client.execute(post);
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            Log.e(null,"entity"+ entity.toString());
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

logcat的

11-13 12:26:47.495: E/AndroidRuntime(26725): FATAL EXCEPTION: main
11-13 12:26:47.495: E/AndroidRuntime(26725): java.lang.IllegalStateException: Could not execute method of the activity
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.view.View$1.onClick(View.java:3680)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.view.View.performClick(View.java:4191)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.view.View$PerformClick.run(View.java:17229)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.os.Handler.handleCallback(Handler.java:615)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.os.Looper.loop(Looper.java:137)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.app.ActivityThread.main(ActivityThread.java:4960)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at dalvik.system.NativeStart.main(Native Method)
11-13 12:26:47.495: E/AndroidRuntime(26725): Caused by: java.lang.reflect.InvocationTargetException
11-13 12:26:47.495: E/AndroidRuntime(26725):    at java.lang.reflect.Method.invokeNative(Native Method)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at java.lang.reflect.Method.invoke(Method.java:511)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at android.view.View$1.onClick(View.java:3675)
11-13 12:26:47.495: E/AndroidRuntime(26725):    ... 11 more
11-13 12:26:47.495: E/AndroidRuntime(26725): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.uthsms.net/index.php
11-13 12:26:47.495: E/AndroidRuntime(26725):    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-13 12:26:47.495: E/AndroidRuntime(26725):    at com.example.postsamapp.MainActivity.entry(MainActivity.java:63)
11-13 12:26:47.495: E/AndroidRuntime(26725):    ... 14 more

1 个答案:

答案 0 :(得分:1)

试试这个:

httpPost post = new HttpPost("http://www.uthsms.net/index.php?on=sms");

如上所述:没有方案(http)没有主机。

此页面上的<form>操作为:

httpPost post = new HttpPost("http://www.uthsms.net/index.php?on=sms-status");