0

以下は、データをphpアドレスに送信しようとしているアプリのコードスニペットです。ただし、エラーが発生します。私のphpサーバーをチェックすると、表示されません..以下のコードスニペットは次のとおりです。

 public void sendData() {
    String ip = getIpAddress();
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://mysamplephp.php?ip=" + ip + "&date=" + formattedDate + "&appname=amigosmexican"+ "&appid="+ android_id);
    try{
        List<BasicNameValuePair> deviceinfo = new ArrayList<BasicNameValuePair>(3);
         deviceinfo.add(new BasicNameValuePair("id", android_id));
         deviceinfo.add(new BasicNameValuePair("date", formattedDate));
         deviceinfo.add(new BasicNameValuePair("ip", ip));
         httppost.setEntity(new UrlEncodedFormEntity(deviceinfo));  

         HttpResponse response = httpclient.execute(httppost);

         InputStream is = response.getEntity().getContent();
         BufferedInputStream bis = new BufferedInputStream(is);
         ByteArrayBuffer baf = new ByteArrayBuffer(20);
         Log.i("postData", response.toString());
         int current = 0;

         while((current = bis.read()) != -1){
             baf.append((byte)current);
         }



    } catch (ClientProtocolException e) {

    } catch (IOException e) {
        Log.e("log_tag", "Error in http connection ",e);
         }
    }

}

エラーは次のとおりです。

 10-12 11:27:21.484: E/log_tag(733): Error in http connection 
10-12 11:27:21.484: E/log_tag(733): java.net.UnknownHostException: ilyushin.ph
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
10-12 11:27:21.484: E/log_tag(733):     at java.net.InetAddress.getAllByName(InetAddress.java:256)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-12 11:27:21.484: E/log_tag(733):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash.sendData(Splash.java:144)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash$PostTask.doInBackground(Splash.java:124)
10-12 11:27:21.484: E/log_tag(733):     at com.mexican.recipes.Splash$PostTask.doInBackground(Splash.java:1)
10-12 11:27:21.484: E/log_tag(733):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-12 11:27:21.484: E/log_tag(733):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-12 11:27:21.484: E/log_tag(733):     at java.lang.Thread.run(Thread.java:1019)

編集: 外部 IP を取得するためのスニペット:

 public String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
    NetworkInterface intf = en.nextElement();
    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
        InetAddress inetAddress = enumIpAddr.nextElement();
        if (!inetAddress.isLoopbackAddress()) {
            return inetAddress.getHostAddress().toString();
        }
    }
}
} catch (SocketException ex) {
Log.e("LOG", ex.toString());
}
return null;
}
4

1 に答える 1

0

List paramsでパラメーターを渡す場合、URLにパラメーターを書き込む必要はありません。

この方法でコードを試してください。

public void sendData() {
String ip = getIpAddress();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mysamplephp.php");
try{
    List<BasicNameValuePair> deviceinfo = new ArrayList<BasicNameValuePair>(3);
     deviceinfo.add(new BasicNameValuePair("id", android_id));
     deviceinfo.add(new BasicNameValuePair("date", formattedDate));
     deviceinfo.add(new BasicNameValuePair("ip", ip));
     httppost.setEntity(new UrlEncodedFormEntity(deviceinfo));  

     HttpResponse response = httpclient.execute(httppost);

     InputStream is = response.getEntity().getContent();
     BufferedInputStream bis = new BufferedInputStream(is);
     ByteArrayBuffer baf = new ByteArrayBuffer(20);
     Log.i("postData", response.toString());
     int current = 0;

     while((current = bis.read()) != -1){
         baf.append((byte)current);
     }



} catch (ClientProtocolException e) {

} catch (IOException e) {
    Log.e("log_tag", "Error in http connection ",e);
     }
}

これを試してみてください、それはうまくいくでしょう。詳細については、このデモコードを参照してください

私のwordepressブログ

于 2012-10-12T05:29:37.807 に答える