0

我正在尝试将 POST 数据发送到同一子网中的服务器。以下是我的代码。但在执行代码后,我收到以下错误:“org.apache.http.conn.HttpHostConnectException:连接到http://192.168.xx被拒绝”。

我们已在清单文件中授予访问 Internet 的权限。当我们授予此权限时,每当我们尝试启动它时,应用程序就会终止。当我们从清单文件中删除权限时,应用程序不会终止,但会给出错误“org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.xx denied ”。

package com.example.posthtml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.x.x/Angelhack/app/create_cause.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("title", "gdfg"));
        nameValuePairs.add(new BasicNameValuePair("description", "ddfgdfg"));
        nameValuePairs.add(new BasicNameValuePair("image", "67546456"));
        nameValuePairs.add(new BasicNameValuePair("city", "pune"));
        nameValuePairs.add(new BasicNameValuePair("tag_1", "dfgdfg"));
        nameValuePairs.add(new BasicNameValuePair("tag_2", "gfdgdg"));
        nameValuePairs.add(new BasicNameValuePair("tag_3", "fddfhfg"));


        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println("Executed\n");
        Toast.makeText(getApplicationContext(), "Perfect",
               Toast.LENGTH_LONG).show();   
    } catch (ClientProtocolException e) {
        Toast.makeText(getApplicationContext(),e.toString(), 
                   Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), e.toString(), 
                   Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
    }
} 

}

这是日志猫

// This i11-08 14:43:36.642: D/AndroidRuntime(1952): Shutting down VM
11-08 14:43:36.642: W/dalvikvm(1952): threadid=1: thread exiting with uncaught exception (group=0xb3a82b90)
11-08 14:43:36.652: E/AndroidRuntime(1952): FATAL EXCEPTION: main
11-08 14:43:36.652: E/AndroidRuntime(1952): Process: com.example.posthtml, PID: 1952
11-08 14:43:36.652: E/AndroidRuntime(1952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.posthtml/com.example.posthtml.MainActivity}: android.os.NetworkOnMainThreadException
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.os.Looper.loop(Looper.java:137)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at java.lang.reflect.Method.invoke(Method.java:515)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at dalvik.system.NativeStart.main(Native Method)
11-08 14:43:36.652: E/AndroidRuntime(1952): Caused by: android.os.NetworkOnMainThreadException
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at libcore.io.IoBridge.connect(IoBridge.java:112)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at java.net.Socket.connect(Socket.java:843)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at com.example.posthtml.MainActivity.postData(MainActivity.java:62)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at com.example.posthtml.MainActivity.onCreate(MainActivity.java:26)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.Activity.performCreate(Activity.java:5243)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-08 14:43:36.652: E/AndroidRuntime(1952):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-08 14:43:36.652: E/AndroidRuntime(1952):     ... 11 more
4

0 回答 0