gpt4 book ai didi

java - 从 android 连接到 MySQL 数据库。没有迹象表明它正在工作/不工作......?

转载 作者:行者123 更新时间:2023-11-29 00:41:36 26 4
gpt4 key购买 nike

我已经通过两个教程来获取我这个项目的代码;

Connecting to MySQL database

Connecting Android to remote mysql via PHP and JSON

我已经学习了一定程度的代码,因为我还是一个初学者并且同时使用了很多代码。这是我目前拥有的:

package com.android.history;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CurrentSeasonDrivers extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentseason_drivers);
}

//PARSE JSON ETC

public void parseJSON() {

String result = "";
String drivername = "";
String drivesfor = "";
InputStream is=null;


//HTTP POST REQUEST
try{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.13/testdatabase.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getBaseContext(), "Could not connect", Toast.LENGTH_LONG).show();

}

//CONVERT DATA TO STRING
try{

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();

}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getBaseContext(), "Could not convert result", Toast.LENGTH_LONG).show();
}

//PARSE JSON DATA
try{

JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;

for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);

drivername=json_data.getString("Driver_full_name");
drivesfor=json_data.getString("Drives_for");

}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getBaseContext(), "Could not parse data", Toast.LENGTH_LONG).show();
}


}
}

现在我没有错误,应用程序工作正常。当我到达这一切的页面时,我什么也得不到。空白页。正如您从我的代码中看到的那样,我添加了 toasts 异常,但它们都没有出现。即使我故意关闭我的服务器也很奇怪。还有什么我应该做的吗?正如标题所说,因为 toast 不起作用,所以我不知道代码是否真的在做任何事情。我只有一个空白页。

我已将 Internet 添加到我的 list 中以允许应用程序访问它。如果我通过浏览器访问代码中的 URL (192.168.0.13/testdatabase.php),数据库中的数据就会正常显示。

编辑:我最终想要的总体结果是显示数据库中的一些数据供我的用户查看。而不是将其作为静态文本放入,而不必为了更新一些数据而更新整个应用程序。

最佳答案

对于 API 级别 11 或更高级别,您需要在单独的线程上实现网络连接。查看此链接:HTTP Client API level 11 or greater in Android .

同样对于 POST 请求,我认为您可以使用此代码:

public String post(String str) {
String result = "";
try {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost postRequest = new HttpPost(SERVER_ADDRESS);
StringEntity input = new StringEntity(str);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
result = getResult(response).toString();
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}

private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
String output;
while ((output = br.readLine()) != null)
result.append(output);

return result;
}

关于java - 从 android 连接到 MySQL 数据库。没有迹象表明它正在工作/不工作......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com