我试图在android中创建一个应用程序,它可以在点击按钮时将一些数据发布到php服务器上。点击该按钮后,该应用程序没有任何反应。日志没有显示任何连接尝试。
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
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.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new httpier().execute();
}
});
}
private class httpier extends AsyncTask<String, Void, Double> {
@Override
protected Double doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://httptestingjawzf.host56.com/androidpost.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("vehicleId", "abc"));
nameValuePairs.add(new BasicNameValuePair("latitude", "abc"));
nameValuePairs.add(new BasicNameValuePair("longitude", "abc"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (NullPointerException n) {
}
return null;
}
}
@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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trial.jawzf.myapplication" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我点击按钮时的日志:
02-03 13:52:59.034 29911-29911/com.trial.jawzf.myapplication D/dalvikvm﹕ GC_EXPLICIT freed 763K, 18% free 12555K/15156K, paused 6ms+4ms, total 62ms
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:52:59.049 29911-29920/com.trial.jawzf.myapplication I/System.out﹕ FinalizerDaemon calls detatch()
02-03 13:53:28.769 29911-29911/com.trial.jawzf.myapplication I/HWUI﹕ EGLImpl-HWUI Protected EGL context created
02-03 13:53:43.039 29911-29911/com.trial.jawzf.myapplication E/OpenGLRenderer﹕ SFEffectCache:clear(), mSize = 0
02-03 13:53:52.469 1443-1458/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:48 height:48 bitmap id is 180
02-03 13:53:52.469 1443-1458/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 181
02-03 13:53:52.474 1443-1458/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:42 height:126 bitmap id is 182
02-03 13:53:52.474 1443-1458/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 183
02-03 13:53:52.474 1443-1459/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 184
02-03 13:53:52.474 1443-1458/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 185
02-03 13:53:52.474 1443-1459/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 186
02-03 13:53:52.479 1443-1443/com.trial.jawzf.myapplication E/dalvikvm﹕ dvmPauseGc(AppLaunch) called - cookie=0x9c28 (f=0x1)
02-03 13:53:52.484 1443-1460/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:78 height:96 bitmap id is 187
02-03 13:53:52.484 1443-1462/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:78 height:96 bitmap id is 188
02-03 13:53:52.484 1443-1464/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 189
02-03 13:53:52.484 1443-1456/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:78 height:96 bitmap id is 190
02-03 13:53:52.484 1443-1443/com.trial.jawzf.myapplication I/PersonaManager﹕ getPersonaService() name persona_policy
02-03 13:53:52.494 1443-1461/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:78 height:96 bitmap id is 191
02-03 13:53:52.504 1443-1463/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:78 height:96 bitmap id is 192
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11341: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 9035: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-03 13:53:52.534 1443-1443/com.trial.jawzf.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
02-03 13:53:52.539 1443-1443/com.trial.jawzf.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 13:53:52.549 1443-1443/com.trial.jawzf.myapplication D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 193
02-03 13:53:52.604 1443-1443/com.trial.jawzf.myapplication I/﹕ PLATFORM VERSION : JB-MR-2
02-03 13:53:52.619 1443-1443/com.trial.jawzf.myapplication I/HWUI﹕ EGLImpl-HWUI Protected EGL context created
02-03 13:53:52.659 1443-1443/com.trial.jawzf.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
02-03 13:53:52.709 1443-1443/com.trial.jawzf.myapplication E/dalvikvm﹕ dvmResumeGc(0x9c28, 0) called (f=0x1)
02-03 13:53:55.104 1443-1480/com.trial.jawzf.myapplication I/System.out﹕ Thread-29082(ApacheHTTPLog):Reading from variable values from setDefaultValuesToVariables
02-03 13:53:55.109 1443-1480/com.trial.jawzf.myapplication I/System.out﹕ Thread-29082(ApacheHTTPLog):isShipBuild true
02-03 13:53:55.109 1443-1480/com.trial.jawzf.myapplication I/System.out﹕ Thread-29082(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
02-03 14:38:03.379 14577-14779/com.trial.jawzf.myapplication D/Http Post Response:﹕ org.apache.http.message.BasicHttpResponse@42678660
这是我正在使用的php文件:
<?php
include 'database.php';
$vID = $_POST['vehicleId'];
$latitude=$_POST['latitude'];
$longitude=$_POST['longitude'];
$userid = $_POST['uid'];
$sqlline = " INSERT INTO $userid VALUES ('$vID','$latitude','$longitude')";
$result=mysql_query($sqlline);
?>
这个php文件是否正确?
答案 0 :(得分:0)
我使用以下代码解决了问题:
final TextView hello=(TextView)findViewById(R.id.hello);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://httptestingjawzf.host56.com/androidpost.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("vehicleId", "abc"));
nameValuePairs.add(new BasicNameValuePair("latitude", "abc"));
nameValuePairs.add(new BasicNameValuePair("longitude", "abc"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("TAG", "Response: " + responseStr);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (NullPointerException n) {
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
使用getEntity()函数!!