gpt4 book ai didi

java - httpclient (phpmyadmin) 不适用于 Android 4.0+

转载 作者:行者123 更新时间:2023-12-01 18:57:49 24 4
gpt4 key购买 nike

我使用下面的代码,它在 Android 2.3.3 中完美运行。然而,在 4.0+ 中它无法以某种方式连接到数据库。我看到一些关于你需要在异步类中获取它的帖子。我也尝试过,但我似乎无法工作。我可能用错了,但我很难理解。

 public class connector extends Activity {
/** Called when the activity is first created. */

TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getServerData(null);
}
//i use my real ip here

public String getServerData(String returnString) {
System.out.println("going to connector class");
InputStream is = null;
final String KEY_121 = "http://10.0.0.128/connector.php";
String result = "";
//the year data to send
// ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("year","1970"));

//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
// 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());
}

//convert response 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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID: "+json_data.getInt("ID")+
", \nActara: "+json_data.getString("Actara")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}

}

Logcat 错误(4.0+ 上):

11-12 12:02:35.658: E/log_tag(14083): Error in http connection android.os.NetworkOnMainThreadException
11-12 12:02:35.658: E/log_tag(14083): Error converting result java.lang.NullPointerException
11-12 12:02:35.663: E/log_tag(14083): Error parsing data org.json.JSONException: End of input at character 0 of

只有第一个错误行很重要,因为它无法连接到数据库,它给出一个 nullPointer(第二个和第三个错误)。

这是我在异步中尝试的:

    public class connector extends Activity {
/** Called when the activity is first created. */

TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new BackgroundAsyncTask().execute();

}

public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {

InputStream is = null;
final String KEY_121 = "http://10.0.0.128/connector.php";
String result = "";
String returnString = "";

protected void onPostExecute(Void result) {

}

@Override
protected void onPreExecute() {
System.out.println("onPreExecute");
}

protected Void doInBackground(String... params) {
try{
System.out.println("background in progress");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
// 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());
}

//convert response 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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID: "+json_data.getInt("ID")+
", \nActara: "+json_data.getString("Actara")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}

protected void onProgressUpdate(Integer... values) {

}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}

}

}

有人可以帮助我吗?我不确定它不适用于 4.0+ 的真正原因。 如果您需要更多信息,请尽管说,我会发布。
代码可能有点困惑,我还没有真正正确地“清理”它。

最佳答案

从 Android 3.0 开始,您不允许在主线程上执行网络操作。为什么?因为网络问题会导致ui慢。所以你必须在一个新线程中完成所有 http 的事情。您走在正确的道路上,但在 AsyncTask 中犯了一个错误。删除异步任务中的空 doInBackground 方法,并在您的方法上写入 @Override。

关于java - httpclient (phpmyadmin) 不适用于 Android 4.0+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13342638/

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