从Android应用程序向服务器发送数据时拒绝连接

时间:2015-01-26 16:22:31

标签: java android phpmyadmin

我是Android应用程序开发的新手,我想发送三个参数Latitude, 经度和Bimari(我在提到 我的代码如下)通过互联网到phpMyAdmin服务器。 问题是从选择bimari后 微调器,当我点击发送按钮我的应用程序崩溃。 我的服务器端工作正常。任何人都可以 请告诉我为什么我无法将数据发送到服务器?

非常感谢您的阅读!

MainActivity.java如下: -

public final static String EXTRA_MESSAGE = "com.example.sushma.myfirstapp.MESSAGE";


String nn, nn1, nn2, nn3, nn4, nn5, nnf;  // used to make URL
String sss1, sss2;  // used to convert lat and long to string
String s; // used to save bimari as string

double lat, lng;
LocationManager lm;

String provider;
Location l;
private View view;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    Criteria c = new Criteria();
    provider = lm.getBestProvider(c, false);
    l = lm.getLastKnownLocation(provider);

    if (l != null) {

     //get latitude and longitude of the location
     lng = l.getLongitude();
     lat = l.getLatitude();

     } else {
    System.out.println("No lat/long");

    }

    {


        LinearLayout l1 = new LinearLayout(this);
        l1.setBackgroundResource(R.drawable.ambulance);
        setContentView(R.layout.activity_main);
    }
}


public void ongobuttonclick(View view) {
    this.view = view;

    Spinner sp = (Spinner) findViewById(R.id.problems);

    s = sp.getSelectedItem().toString();
    sss1 = String.valueOf(lng);
    sss2 = String.valueOf(lat);

    nn = "http://192.168.2.8/tech/new_predict.php?Latitude=";
    nn1 = sss2;
    nn2 = "&Longitude=";
    nn3 = sss1;
    nn4 = "&Bimari=";
    nn5 = s;

    nnf = nn + nn1 + nn2 + nn3 + nn4 + nn5;

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httPost = new HttpPost(nnf);
    System.out.println(nnf);
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                httpclient.execute(httPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

@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);
}

Logcat

01-26 19:32:22.859  14247-14247/com.example.sushma.myfirstapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-26 19:32:29.085  14247-14247/com.example.sushma.myfirstapp D/AndroidRuntime﹕ Shutting down VM
01-26 19:32:29.085  14247-14247/com.example.sushma.myfirstapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40da9930)
01-26 19:32:29.105  14247-14247/com.example.sushma.myfirstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3804)
        at android.view.View.performClick(View.java:4439)
        at android.widget.Button.performClick(Button.java:139)
        at android.view.View$PerformClick.run(View.java:18395)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3799)
        at android.view.View.performClick(View.java:4439)
        at android.widget.Button.performClick(Button.java:139)
        at android.view.View$PerformClick.run(View.java:18395)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
        at libcore.io.IoBridge.connect(IoBridge.java:112)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
        at java.net.Socket.connect(Socket.java:842)
        at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at com.example.sushma.myfirstapp.MainActivity.ongobuttonclick(MainActivity.java:176)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3799)
        at android.view.View.performClick(View.java:4439)
        at android.widget.Button.performClick(Button.java:139)
        at android.view.View$PerformClick.run(View.java:18395)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

NewLogcat

01-26 22:45:48.807  23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-26 22:45:48.807  23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11750: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11756: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9401: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-26 22:45:48.867  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 534: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp W/dalvikvm﹕ VFY: unable to resolve virtual method 556: Landroid/content/res/TypedArray;.getType (I)I
01-26 22:45:48.887  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
01-26 22:45:48.947  23318-23318/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_FOR_ALLOC freed 157K, 13% free 8676K/9872K, paused 24ms, total 24ms
01-26 22:45:48.957  23318-23318/com.example.sushma.myfirstapp I/dalvikvm-heap﹕ Grow heap (frag case) to 10.654MB for 1228816-byte allocation
01-26 22:45:48.967  23318-23329/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 11% free 9875K/11076K, paused 14ms, total 14ms
01-26 22:45:49.007  23318-23323/com.example.sushma.myfirstapp E/dalvikvm﹕ adjustAdaptiveCoef max=4194304, min=1048576, ut=568
01-26 22:45:49.007  23318-23323/com.example.sushma.myfirstapp D/dalvikvm﹕ GC_CONCURRENT freed 4K, 11% free 9871K/11076K, paused 16ms+3ms, total 40ms
01-26 22:45:49.257  23318-23318/com.example.sushma.myfirstapp D/libEGL﹕ loaded /system/lib/egl/libGLES_hawaii.so
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ mem_init ++
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ gHwMemAllocator client 3
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ **** Using ION allocator ****
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ registered SIGUSR1[10] for pid[23318]
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ HwMemAllocatorImpl Static Counters 0 0
01-26 22:45:49.267  23318-23318/com.example.sushma.myfirstapp D/﹕ HwMemAllocatorImpl[490a9dcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
01-26 22:45:49.277  23318-23318/com.example.sushma.myfirstapp D/﹕ mem_init 490a9dcc--
01-26 22:45:49.277  23318-23318/com.example.sushma.myfirstapp D/ION﹕ config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
01-26 22:45:49.277  23318-23318/com.example.sushma.myfirstapp D/MM_DEVICE﹕ Waiting for mm thread to come up
01-26 22:45:49.277  23318-23346/com.example.sushma.myfirstapp D/MM_DEVICE﹕ mm_device_thread starting
01-26 22:45:49.277  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateContext() config: 18 context: 0x4f3c4db8, VC context 1, Thread 23318
01-26 22:45:49.277  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
01-26 22:45:49.287  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4beeea48, VC surface: 1, Thread: 23318
01-26 22:45:49.287  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:49.287  23318-23318/com.example.sushma.myfirstapp D/OpenGLRenderer﹕ Enabling debug mode 0
01-26 22:45:50.849  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ Get MotionRecognitionManager
01-26 22:45:50.959  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ Set SWAP INTERVAL 0
01-26 22:45:50.969  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglCreateWindowSurface() surface: 0x4c0f1be8, VC surface: 2, Thread: 23318
01-26 22:45:50.969  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:50.989  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:50.999  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:50.999  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:51.079  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ unregisterIRListener() is called
01-26 22:45:51.089  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:52.150  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4c0f1be8, 0x4c0f1be8) Thread: 23318
01-26 22:45:52.791  23318-23318/com.example.sushma.myfirstapp D/AbsListView﹕ onDetachedFromWindow
01-26 22:45:52.791  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(NULL) Thread: 23318
01-26 22:45:52.791  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglDestroySurface() surface: 0x4c0f1be8, android window 0x4c05c8f8, Thread: 23318
01-26 22:45:52.811  23318-23318/com.example.sushma.myfirstapp D/HAWAII_EGL﹕ eglMakeCurrent(0x4f3c4db8, 0x4beeea48, 0x4beeea48) Thread: 23318
01-26 22:45:52.871  23318-23318/com.example.sushma.myfirstapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-26 22:45:53.802  23318-23318/com.example.sushma.myfirstapp I/System.out﹕ http://192.168.2.8/tech/new_predict.php?Latitude=31.310573780338718&Longitude=75.55498407042245&Bimari=Bones
01-26 22:45:56.945  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.2.8 refused
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at  org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at com.example.sushma.myfirstapp.MainActivity$1.run(MainActivity.java:173)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
01-26 22:45:56.955  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /192.168.2.8 (port 80): connect failed: EHOSTUNREACH (No route to host)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at java.net.Socket.connect(Socket.java:842)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at     org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ ... 8 more
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.Posix.connect(Native Method)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
01-26 22:45:56.965  23318-23498/com.example.sushma.myfirstapp W/System.err﹕ ... 13 more

1 个答案:

答案 0 :(得分:0)

您尝试在UI线程中执行httpclient.execute(httPost);方法。你不能做这个。

您可以使用AsyncTask来解决此问题。

public void ongobuttonclick(View view) {

     ...
    //your code 

    //use AsyncTask if you  want ot inform ui thread
     final HttpClient httpclient = new DefaultHttpClient();
    final HttpPost httPost = new HttpPost(nnf);

    new AsyncTask<HttpPost, Void, HttpResponse>() {
        //YourParams, YourProgress, YourResult can be Void
        @Override
        protected HttpResponse doInBackground(HttpPost... params) {
            HttpResponse response = null;
            try {
               response = httpclient.execute(params[0]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;
        }

        @Override
        protected void onPostExecute(HttpResponse response) {
            if(response != null){
                //do sth in UI thread
            }
        }
    }.execute(httPost);

    //or in new thread if you doesn't want ot inform ui thread
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                httpclient.execute(httPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}