Java Android HttpUrlConnection不工作
基本上,我正在尝试通过Android应用程序连接到web界面 我使用HttpClient成功地向表单发送了命令。但是,我希望按照这里的建议使用HttpUrlConnection来实现这一点,目的是实现更快、更节能的连接Java Android HttpUrlConnection不工作,java,android,httpclient,httpurlconnection,Java,Android,Httpclient,Httpurlconnection,基本上,我正在尝试通过Android应用程序连接到web界面 我使用HttpClient成功地向表单发送了命令。但是,我希望按照这里的建议使用HttpUrlConnection来实现这一点,目的是实现更快、更节能的连接 URL url = new URL("http://" + mIpAddress + ":" + mPort + "/command.html"); HttpURLConnection connection = (HttpURLConnection) url.openConnec
URL url = new URL("http://" + mIpAddress + ":" + mPort + "/command.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(URLEncoder.encode("parameter=" + value, "UTF-8");
writer.flush();
writer.close();
os.close();
connection.connect();
编辑:没有引发异常,因为代码执行得很好,请求可能不是服务器所期望的格式?您可以提供更多信息,如异常或服务器响应吗?服务器是否收到您的请求
但是,我建议您使用在
HttpUrlConnection
之上构建的连接。您可以提供更多信息,如异常或服务器响应吗?服务器是否收到您的请求
但是,我建议您使用在
HttpUrlConnection
之上构建的连接。下面是我使用HttpUrlConnection发送http请求的方法,请注意,connection.connect()是在设置HttpUrlConnection之后,而不是在最后,setDoInput也会发出请求,而不是默认的get
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setReadTimeout(10000 /* milliseconds */);
// conn.setConnectTimeout(15000 /* milliseconds */);
//conn.setRequestMethod("GET");
//conn.setDoInput(true); //* uses POST
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
//String data = convertStreamToString(stream);
int sz = stream.available();
byte[] b = new byte[sz];
stream.read(b);
stream.close();
String data = new String(b);
下面是我使用HttpURLConnection发送http请求的方法,注意connection.connect()是在设置HttpURLConnection之后,而不是在最后,setDoInput也会发出请求,而不是默认的get
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setReadTimeout(10000 /* milliseconds */);
// conn.setConnectTimeout(15000 /* milliseconds */);
//conn.setRequestMethod("GET");
//conn.setDoInput(true); //* uses POST
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
//String data = convertStreamToString(stream);
int sz = stream.available();
byte[] b = new byte[sz];
stream.read(b);
stream.close();
String data = new String(b);
投递工作需要Connection.getInputStream()。已修复。connection.getInputStream()是POST工作所必需的。已修复。只需添加一行即可
connection.getRequestMethod();
它会起作用的
我不知道为什么,但在我花了一整天的时间之后,这对我来说很有效 只需添加一行即可
connection.getRequestMethod();
它会起作用的
我不知道为什么,但在我花了一整天的时间之后,这对我来说很有效 使用HttpUrlConnection类共享数据 下面是一个简单的连接类,它将帮助您连接WS,并使用JSON解析传递数据
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CommonConnectionClass {
JSONObject jsonObject;
public JSONObject getConnection(String u,JSONObject object){
try {
//URL url=new URL("https://e-gurukulam.000webhostapp.com/public_html/project/login.php"+u+".php");
URL url=new URL("http://"+new Connection_Url_Setter().urlIp+"/classroom/"+u+".php");
// URL url=new URL("http://192.168.43.120/classroom/"+u+".php");
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type","application/json; charset=utf-8");
httpURLConnection.setRequestProperty("Accept","application/json; charset=utf-8");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
if(object!=null) {
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(object.toString().getBytes());
}
int code=httpURLConnection.getResponseCode();
if(code== HttpURLConnection.HTTP_OK){
String inputLine;
StringBuilder stringBuilder=new StringBuilder();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while((inputLine=bufferedReader.readLine())!=null){
stringBuilder.append(inputLine);
}
jsonObject=new JSONObject(stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
}
要使用这个类,我使用Asyn类,比如
class userNameFetchTask extends AsyncTask<Void, Void, JSONObject> {
JSONObject object, jsonObject, json;
JSONArray jsonArray;
ArrayList<CreateGroupModel> lidList = new ArrayList<>();
String lid, name;
@Override
protected void onPreExecute() {
jsonObject = new JSONObject();
try {
jsonObject.put("lid", sp.getString("lid", null));
} catch (JSONException e) {
e.printStackTrace();
}
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(Void... voids) {
object = new JSONObject();
CommonConnectionClass commonConnectionClass = new CommonConnectionClass();
object = commonConnectionClass.getConnection("selectUserForGroup", jsonObject);
return object;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
jsonArray = new JSONArray();
try {
jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
CreateGroupModel model = new CreateGroupModel();
json = jsonArray.getJSONObject(i);
model.setLid(json.getString("relative_lid"));
model.setName(json.getString("name"));
model.setEmail(json.getString("email"));
model.setImage(json.getString("image"));
lidList.add(model);
}
viewstudentsadapterForCreateGroup viewstudentsadapterForCreateGroup = new viewstudentsadapterForCreateGroup(getApplicationContext(), lidList, createGroup_Activity.this);
listView.setAdapter(viewstudentsadapterForCreateGroup);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
类userNameFetchTask扩展了AsyncTask{
JSONObject对象,JSONObject,json;
JSONArray JSONArray;
ArrayList lidList=新的ArrayList();
字符串盖子,名称;
@凌驾
受保护的void onPreExecute(){
jsonObject=新的jsonObject();
试一试{
jsonObject.put(“lid”,sp.getString(“lid”,null));
}捕获(JSONException e){
e、 printStackTrace();
}
super.onPreExecute();
}
@凌驾
受保护的JSONObject doInBackground(无效…无效){
object=新的JSONObject();
CommonConnectionClass CommonConnectionClass=新的CommonConnectionClass();
object=commonConnectionClass.getConnection(“selectUserForGroup”,jsonObject);
返回对象;
}
@凌驾
受保护的void onPostExecute(JSONObject JSONObject){
super.onPostExecute(jsonObject);
jsonArray=新的jsonArray();
试一试{
jsonArray=jsonObject.getJSONArray(“响应”);
for(int i=0;i
使用HttpUrlConnection类共享数据
下面是一个简单的连接类,它将帮助您连接WS,并使用JSON解析传递数据
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CommonConnectionClass {
JSONObject jsonObject;
public JSONObject getConnection(String u,JSONObject object){
try {
//URL url=new URL("https://e-gurukulam.000webhostapp.com/public_html/project/login.php"+u+".php");
URL url=new URL("http://"+new Connection_Url_Setter().urlIp+"/classroom/"+u+".php");
// URL url=new URL("http://192.168.43.120/classroom/"+u+".php");
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type","application/json; charset=utf-8");
httpURLConnection.setRequestProperty("Accept","application/json; charset=utf-8");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
if(object!=null) {
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(object.toString().getBytes());
}
int code=httpURLConnection.getResponseCode();
if(code== HttpURLConnection.HTTP_OK){
String inputLine;
StringBuilder stringBuilder=new StringBuilder();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while((inputLine=bufferedReader.readLine())!=null){
stringBuilder.append(inputLine);
}
jsonObject=new JSONObject(stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
}
要使用这个类,我使用Asyn类,比如
class userNameFetchTask extends AsyncTask<Void, Void, JSONObject> {
JSONObject object, jsonObject, json;
JSONArray jsonArray;
ArrayList<CreateGroupModel> lidList = new ArrayList<>();
String lid, name;
@Override
protected void onPreExecute() {
jsonObject = new JSONObject();
try {
jsonObject.put("lid", sp.getString("lid", null));
} catch (JSONException e) {
e.printStackTrace();
}
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(Void... voids) {
object = new JSONObject();
CommonConnectionClass commonConnectionClass = new CommonConnectionClass();
object = commonConnectionClass.getConnection("selectUserForGroup", jsonObject);
return object;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
jsonArray = new JSONArray();
try {
jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
CreateGroupModel model = new CreateGroupModel();
json = jsonArray.getJSONObject(i);
model.setLid(json.getString("relative_lid"));
model.setName(json.getString("name"));
model.setEmail(json.getString("email"));
model.setImage(json.getString("image"));
lidList.add(model);
}
viewstudentsadapterForCreateGroup viewstudentsadapterForCreateGroup = new viewstudentsadapterForCreateGroup(getApplicationContext(), lidList, createGroup_Activity.this);
listView.setAdapter(viewstudentsadapterForCreateGroup);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
类userNameFetchTask扩展了AsyncTask{
JSONObject对象,JSONObject,json;
JSONArray JSONArray;
ArrayList lidList=新的ArrayList();
字符串盖子,名称;
@凌驾
受保护的void onPreExecute(){
jsonObject=新的jsonObject();
试一试{
jsonObject.put(“lid”,sp.getString(“lid”,null));
}捕获(JSONException e){
e、 printStackTrace();
}
super.onPreExecute();
}
@凌驾
受保护的JSONObject doInBackground(无效…无效){
object=新的JSONObject();
CommonConnectionClass CommonConnectionClass=新的CommonConnectionClass();
object=commonConnectionClass.getConnection(“selectUserForGroup”,jsonObject);
返回对象;
}
@凌驾
受保护的void onPostExecute(JSONObject JSONObject){
super.onPostExecute(jsonObject);
jsonArray=新的jsonArray();
试一试{
jsonArray=jsonObject.getJSONArray(“响应”);
for(int i=0;i