无法将数据从Android发送到PHP

时间:2012-10-11 08:38:23

标签: php android http tracking

我正在尝试将日期从Android发送到PHP。我在启动画面活动中添加了一些代码,以便能够做到这一点。但是,它给了我一个错误。我不知道我错过了什么。以下是完整的代码:

 public class Splash extends Activity {

DBAdapter db = new DBAdapter(this);
protected int _splashTime = 5000; 
private Thread splashTread;
private String android_id;
private String formattedDate;
SharedPreferences prefs = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);



    Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("MM'/'dd'/'yyyy");
    formattedDate = df.format(c.getTime());


    android_id =Secure.getString(this.getContentResolver(),
            Secure.ANDROID_ID); 

    Log.d("IP", getIpAddress());
    Log.d("id", android_id);
    Log.d("date",""+ formattedDate);


    final Splash sPlashScreen = this; 


    splashTread = new Thread() {
        @Override
        public void run() {
            try {                   
                synchronized(this){
                    wait(_splashTime);
                }

            } catch(InterruptedException e) {} 
            finally {
                finish();

                Intent i = new Intent();
                i.setClass(sPlashScreen, Tab.class);
                startActivity(i);

                try {
                    splashTread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    splashTread.start();
    prefs = getSharedPreferences("com.sample.package", MODE_PRIVATE);


}


@Override
public void onBackPressed() {
}
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;
}

@Override
protected void onResume() {
super.onResume();

if (prefs.getBoolean("firstrun", true)) {
    prefs.edit().putBoolean("firstrun", false).commit();
    Log.d("FIRST LAUNCH?", "YES");
    sendData();

}else{
    Log.d("FIRST LAUNCH?", "NO");
 }
}


public void sendData() {
HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://whatever.php");
try{
    List<BasicNameValuePair> deviceInfo = new ArrayList<BasicNameValuePair>(3);
       deviceInfo.add(new BasicNameValuePair("ip", getIpAddress()));
       deviceInfo.add(new BasicNameValuePair("date", formattedDate));
       deviceInfo.add(new BasicNameValuePair("id", android_id));
       httppost.setEntity(new UrlEncodedFormEntity(deviceInfo));
       HttpResponse response = httpclient.execute(httppost);
       HttpEntity entity = response.getEntity();
       InputStream is = entity.getContent();
       Log.i("postData", response.getStatusLine().toString());
 } catch (IOException e) {
      Log.e("log_tag", "Error in http connection "+e.toString());

 }

}
}

我发布了活动的所有代码,因此更容易理解。

编辑:

10-11 17:10:14.885: E/log_tag(352): Error in http connection 
10-11 17:10:14.885: E/log_tag(352): java.net.UnknownHostException: xxxxxxxx.ph
10-11 17:10:14.885: E/log_tag(352):     at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
10-11 17:10:14.885: E/log_tag(352):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
10-11 17:10:14.885: E/log_tag(352):     at java.net.InetAddress.getAllByName(InetAddress.java:256)
10-11 17:10:14.885: E/log_tag(352):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
 10-11 17:10:14.885: E/log_tag(352):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-11 17:10:14.885: E/log_tag(352):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
 10-11 17:10:14.885: E/log_tag(352):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
10-11 17:10:14.885: E/log_tag(352):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-11 17:10:14.885: E/log_tag(352):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-11 17:10:14.885: E/log_tag(352):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-11 17:10:14.885: E/log_tag(352):     at com.mexican.recipes.Splash.sendData(Splash.java:142)
10-11 17:10:14.885: E/log_tag(352):     at    com.mexican.recipes.Splash.onResume(Splash.java:125)
10-11 17:10:14.885: E/log_tag(352):     at   com.mexican.recipes.Splash.onCreate(Splash.java:92)
10-11 17:10:14.885: E/log_tag(352):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-11 17:10:14.885: E/log_tag(352):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-11 17:10:14.885: E/log_tag(352):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-11 17:10:14.885: E/log_tag(352):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-11 17:10:14.885: E/log_tag(352):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-11 17:10:14.885: E/log_tag(352):     at android.os.Handler.dispatchMessage(Handler.java:99)
 10-11 17:10:14.885: E/log_tag(352):    at android.os.Looper.loop(Looper.java:123)
10-11 17:10:14.885: E/log_tag(352):     at     android.app.ActivityThread.main(ActivityThread.java:3683)
10-11 17:10:14.885: E/log_tag(352):     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 17:10:14.885: E/log_tag(352):     at java.lang.reflect.Method.invoke(Method.java:507)
10-11 17:10:14.885: E/log_tag(352):     at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-11 17:10:14.885: E/log_tag(352):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-11 17:10:14.885: E/log_tag(352):     at dalvik.system.NativeStart.main(Native Method)

此外,它不再显示我的启动画面。

编辑:

这些是我的权限:

 <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

0 个答案:

没有答案