0

这是我的代码我试图连接到服务器数据库但给出异常socketexception网络nt可达但它适用于localhost请帮助我wat是dis代码中的问题

package com.example.test2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

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.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

//import android.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
//import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    TextView resultView;
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resultView = (TextView) findViewById(R.id.result);
        //StrictMode.enableDefaults();
        getData();
    }

    public void getData(){
        String result = "";
        InputStream isr = null;
        try{
            HttpClient httpclient = new DefaultHttpClient();
            Toast.makeText(getApplicationContext()  , "1"   ,Toast.LENGTH_SHORT).show();

            HttpPost httppost = new HttpPost("http://localhost/xampp/connect.php");
            String str=httppost.toString();//YOUR PHP SCRIPT ADDRESS 
            Toast.makeText(getApplicationContext()  , str   ,Toast.LENGTH_LONG).show();

            HttpResponse response = httpclient.execute(httppost);

            Toast.makeText(getApplicationContext()  , "3"   ,Toast.LENGTH_SHORT).show();

            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
    }
    catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
            resultView.setText("Couldnt connect to database");
    }
    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            isr.close();

            result=sb.toString();
    }
    catch(Exception e){
            Log.e("log_tag", "Error  converting result "+e.toString());
    }

    //parse json data
   try {
       String s = "";
       JSONArray jArray = new JSONArray(result);

       for(int i=0; i<jArray.length();i++){
           JSONObject json = jArray.getJSONObject(i);
           s = s + 
                   "ID : "+json.getString("id")+" \nNAME : "+json.getString("name")+"\n\n";
       }

       resultView.setText(s);

   } catch (Exception e) {
    // TODO: handle exception
       Log.e("log_tag", "Error Parsing Data "+e.toString());
   }

    }


}

即使我获得了 Internet 许可,我仍然需要任何许可

4

3 回答 3

2
"http://localhost/xampp/connect.php" 

你不能把本地主机而是把你的IP地址放在那里。

于 2013-08-19T22:09:13.990 回答
1

问题出在这里:

HttpPost httppost = new HttpPost("http://localhost/xampp/connect.php");

将 localhost 更改为您的 IP 地址,如下所示:

HttpPost httppost = new HttpPost("your ip address /connect.php");// where your ip address will be something like 192.168.14.4

要获取您的 IP 地址,请转到开始> 键入 cmd>,然后在出现的屏幕中键入 ipconfig。如果您连接到 WIFI,请在 IPV4 下的 WIFI 下查看。还要确保你把服务器放在网上!

于 2013-08-19T04:38:26.520 回答
0

如果你把 getData() 放在 Thread 中,问题就会得到解决。

例子:

new Thread(new Runnable(){
    @Override
    public void run() {
        getData(); // If you want to update UI, please put Update UI into runOnUIThread
    }
});

但我不建议你使用这种方式。请使用 AsyncTask 来解决。

阅读更多我在这个问题中的答案:AsyncTask 和 Activity 之间的关系是什么?

于 2013-08-19T03:39:25.100 回答