gpt4 book ai didi

java - Android 中线程的实现不起作用

转载 作者:可可西里 更新时间:2023-11-01 08:28:43 26 4
gpt4 key购买 nike

我正在 Android 中编写一个基本应用程序,该应用程序将通过 PHP 中的请求连接到 MySql 服务器,在 Android 中互联网连接必须在不同的线程中进行,因此我创建了实现 Runnable 接口(interface)的类。

package com.company.opax.loginmysql;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

/**
* Created by opax on 30.08.2015.
*/

public class HttpPostMethod implements Runnable{
private String fileInHost;
private ArrayList<PostParameters> postParameterses;
private StringBuffer postResult;

public HttpPostMethod(String fileInHost, ArrayList<PostParameters> postParameterses){
this.fileInHost = fileInHost;
this.postParameterses = new ArrayList<PostParameters>(postParameterses);
}

public String getResult() {
return postResult.toString();
}

@Override
public void run() {
try {
String urlParameters = generateParameters();
URLConnection conn = initializeUrlConnection();

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(urlParameters);
writer.flush();

String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

while ((line = reader.readLine()) != null) {
postResult.append(line);
}

writer.close();
reader.close();
} catch (IOException e) {
Log.e("Exception", this.getClass().getName() + " name: " + e.toString());
}
}

private URLConnection initializeUrlConnection() throws MalformedURLException {
URL url = new URL(fileInHost);
URLConnection conn;

try {
conn = url.openConnection();
conn.setDoOutput(true);
}catch(IOException e){
throw new MalformedURLException();
}

return conn;
}

private String generateParameters(){
StringBuffer finishPostQuery = new StringBuffer();

for(PostParameters p : postParameterses){
finishPostQuery.append(p.getNameParam());
finishPostQuery.append("=");
finishPostQuery.append(p.getValueParam());
finishPostQuery.append("&");
}

if(!finishPostQuery.toString().equals("login=seba&password=pass&"))
throw new AssertionError("blad generatora zapytania: " + finishPostQuery);

return finishPostQuery.toString();
}
}

登录类:

public class Login {
private User user;
private final String paramLogin = "login";
private final String paramPass = "password";

public Login(User user){
this.user = user;
}

public boolean tryLogin(){
try{
ArrayList<PostParameters> postParameterses = new ArrayList<>();
postParameterses.add(new PostParameters(paramLogin, user.getUserName()));
postParameterses.add(new PostParameters(paramPass, user.getUserPass()));

HttpPostMethod httpPostMethod = new HttpPostMethod("http://blaba.php", postParameterses);
httpPostMethod.run();
Log.i("bla", httpPostMethod.getResult());

} catch (Exception e) {
Log.i("Exception", e.toString());
}
return false;
}
}

我正在尝试连接到其他线程,但我仍然遇到错误:'android.os.NetworkOnMainThreadException'对于我做错的所有建议,我将不胜感激。

最佳答案

代替:

httpPostMethod.run();

做:

new Thread(httpPostMethod).start();

如果您的登录调用由于某些原因(超时、登录错误)失败,您应该以某种方式向用户报告——这就是 AsyncTask 类的用途。它允许您在 doInBackkground 中运行后台代码,并且在网络操作结束后 - 在 onPostExecute 中,您可以执行 UI 相关的东西 - 如显示错误/结果。

关于java - Android 中线程的实现不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314149/

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