gpt4 book ai didi

mysql - 在android studio中使用URLConnection代替HttpClient

转载 作者:行者123 更新时间:2023-11-29 21:49:47 24 4
gpt4 key购买 nike

有什么方法可以将此代码转换为 URLCOnnection 而不是使用 Httpclient 吗?我正在使用 API23,并且不再支持 HTTP 客户端。我很难使用 URLCOnnection,因为几乎所有教程都使用 HttpClient 进行连接。谢谢

  public class SigninActivity  extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;

//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
this.context = context;
this.statusField = statusField;
this.roleField = roleField;
byGetOrPost = flag;
}

protected void onPreExecute(){

}

@Override
protected String doInBackground(String... arg0) {
if(byGetOrPost == 0){ //means by Get Method

try{
String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "http://myphpmysqlweb.hostei.com/login.php?username="+username+"& password="+password;

URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer sb = new StringBuffer("");
String line="";

while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}

catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
else{
try{
String username = (String)arg0[0];
String password = (String)arg0[1];

String link="http://myphpmysqlweb.hostei.com/loginpost.php";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

URL url = new URL(link);
URLConnection conn = url.openConnection();

conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

wr.write( data );
wr.flush();

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

StringBuilder sb = new StringBuilder();
String line = null;

// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}

}

    @Override
protected void onPostExecute(String result){
this.statusField.setText("Login Successful");
this.roleField.setText(result);

}}

最佳答案

这是 URLConnection 的基本格式,我将我的一些工作代码与您的代码合并在一起。如果您在我指定的位置填写代码,它应该可以工作。

//-------------------

String username = (String)arg0[0];
String password = (String)arg0[1];
String link = "http://myphpmysqlweb.hostei.com/login.php?username="+username+"& password="+password;

InputStream is = null;

try {

URL url = new URL(link);
URLConnection urlConnection = (HttpURLConnection) url.openConnection();


is = urlConnection.getInputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

//FILL IN YOUR CODE HERE!
//FILL IN YOUR CODE HERE!

} catch(Exception e){
return new String("Exception: " + e.getMessage());
}

//-------------------

关于mysql - 在android studio中使用URLConnection代替HttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762060/

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