对以下任何建议都非常感谢。我看过这个问题的类似帖子,还没有找到适合我的解决方案。作为原理的证明,我试图在我的Android应用程序和一个保存我的MYSQL数据库的远程(我的笔记本电脑)服务器之间建立连接。我已经开发了一个非常简单和基本的应用程序,看看我是否可以让它工作,但到目前为止失败了。所有的应用程序都要求用户键入一个城市,然后按提交将该城市插入数据库。到目前为止,我一直收到以下错误:
org.apache.http.conn.HttpHostConnectException:连接到http:/192.185.1.3:80拒绝。
我知道我的php脚本有效,因为我可以通过创建一个简单的HTML表单来插入数据,从网上插入项目到我的数据库。但我不能从我的Android应用程序中做同样的事情。
我试过了:
禁用防火墙
在android清单中插入INTERNET权限。
请在下面找到以下php和Android文件。
public class AddCityFragment extends Fragment {
private EditText field;
private Button submitButton;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
// url to create new city
private static String url_create_city = "http://192.168.1.3:80/babylonserver/create_city_main.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.addcityfragment, parent, false);
// inflate widgets
field = (EditText) v.findViewById(R.id.enterCityHere);
submitButton = (Button) v.findViewById(R.id.submitButton);
// Adding actionListener
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initiates the insertion of new city into database
// ceate instance of InsertCity()
new InsertCity().execute();
}
});
return v;
}
private class InsertCity extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
// assign what is in field to a string variable
String city = field.getText().toString();
//Build parameter
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("city", city));
// create JSON object
JSONObject json=jsonParser.makeHttpRequest(url_create_city, "POST", params);
return null;
}
}
}
Android清单:
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".AddCity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Success_Activity"
android:label="@string/app_name" >
</activity>
</application>
Create_city_main.php:
<?php
/**
* To insert a row, in field = city, into the database
* database name=python
* table: city_table
*/
// array for JSON response
$response = array();
if(isset($_POST['name']) && !empty($_POST['name'])){
$city = $_POST['name'];
// include db connect class
require_once __DIR__.'/db_connect.php';
// connect to the database
$db = new DB_CONNECT();
// MYSQL inserting new row
$result = mysql_query("INSERT INTO city_table(city_name) VALUES ('$city')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
来自LogCat的文件:
org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.3:80 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:183)
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:374)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
at com.example.addcityapp.JSONParser.makeHttpRequest(JSONParser.java:51)
at com.example.addcityapp.AddCityFragment$InsertCity.doInBackground(AddCityFragment.java:71)
at com.example.addcityapp.AddCityFragment$InsertCity.doInBackground(AddCityFragment.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.net.ConnectException: failed to connect to /192.168.1.3 (port 80): connect failed: ETIMEDOUT
(Connection timed out)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
at java.net.Socket.connect(Socket.java:833)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:144)
... 15 more
Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
... 20 more
Error converting result java.lang.NullPointerException: lock == null
Error parsing data org.json.JSONException: End of input at character 0 of