Android的HttpURLConnection类用JSON字符串 [英] Android HttpURLConnection with json string
问题描述
我要打电话以下网址:
<$p$p><$c$c>http://192.168.0.196:8080/openapi/localuser/set?{\"syskey\":\"1234\",\"usrname\":\"256\",\"usrpwd\":\"556\"}使用这个地址的新用户添加到数据库中。要做到这一点,我用 HttpURLConnection类
在我的的AsyncTask
类
{尝试 网址myUrl =新的URL(PARAMS [0]);
HttpURLConnection的康恩=(HttpURLConnection类)myUrl.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod(POST);
conn.setDoInput(真);
//开始查询
conn.connect();
INT响应= conn.getResponse code(); Log.d(实验室,响应是:+响应); statusMap.put(ADDUSER,Integer.toString(响应)); Log.d(实验室,URL:+参数[0]); }赶上(例外五){
Log.d(实验室,误差2:+ e.getMessage());
}
PARAMS [0] = http://192.168.0.196:8080/openapi/localuser/set?{\"syskey\":\"1234\",\"usrname\":\"256\",\"usrpwd\":\"556\"}$c$c>
不幸的是,这一呼吁是行不通的。我不明白的错误。
抓
返回null
尝试这样的方式。你需要在你的code添加几行。
公开的JSONObject makeHtt prequest(字符串requestURL,JSONObject的寄存器){ 尝试{ URL =新的URL(requestURL); 连接=(HttpURLConnection类)url.openConnection(); connection.setReadTimeout(150000);
connection.setConnectTimeout(150000); connection.setAllowUserInteraction(真);
connection.setRequestMethod(POST);
connection.setRequestProperty(接收字符集,UTF-8);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestProperty(接受语言,EN-US,连接; Q = 0.5);
connection.setFixedLengthStreamingMode(register.toString()的getBytes()的长度。);
connection.setDoInput(真);
connection.setDoOutput(真);
OutputStreamWriter的OutputStream =新OutputStreamWriter(connection.getOutputStream()); outputStream.write(register.toString()); outputStream.flush(); Log.e(URL,connection.getURL()的toString());
Log.e(JSONObject的,register.toString());
}赶上(例外五){
Log.e(主要例外,e.toString());
}
尝试{ INT状态code = connection.getResponse code(); 如果(状态code == HttpURLConnection.HTTP_OK){
是= connection.getInputStream();
}其他{ }
}赶上(IOException异常五){
Log.e(IOException异常,e.toString());
} 尝试{ RD =新的BufferedReader(新的InputStreamReader(是)); 响应=新的StringBuffer(); 而((行= rd.readLine())!= NULL){
response.append(线);
response.append('\\ n');
}
Log.e(回应,response.toString()+); rd.close();
}赶上(IOException异常五){
Log.e(BUFFER_READER,e.toString());
}赶上(NullPointerException异常五){
Log.e(NullPointerException异常,e.toString());
} {最后
connection.disconnect();
} 尝试{
返回新的JSONObject(response.toString());
}赶上(JSONException E){
Log.e(JSONException,e.toString()); }
返回null;
}
另外你正在使用本地主机必须拥有模拟器可以连接到本地主机。除非它不会去任何设备上运行。
I want to call the following URL:
http://192.168.0.196:8080/openapi/localuser/set?{"syskey":"1234","usrname":"256","usrpwd":"556"}
Use this address to add a new user to the database. To do this I use HttpURLConnection
in my AsyncTask
class
try {
URL myUrl = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("lab", "The response is: " + response);
statusMap.put("addUser", Integer.toString(response));
Log.d("lab", "URL: " + params[0]);
}catch (Exception e){
Log.d("lab", "Error2: " + e.getMessage());
}
params[0] = http://192.168.0.196:8080/openapi/localuser/set?{"syskey":"1234","usrname":"256","usrpwd":"556"}
Unfortunately, this call is not working. I do not get the error.
catch
returns null
Try like this way. You need to add few line in your code.
public JSONObject makeHttpRequest(String requestURL, JSONObject register) {
try {
url = new URL(requestURL);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(150000);
connection.setConnectTimeout(150000);
connection.setAllowUserInteraction(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setFixedLengthStreamingMode(register.toString().getBytes().length);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStreamWriter outputStream = new OutputStreamWriter(connection.getOutputStream());
outputStream.write(register.toString());
outputStream.flush();
Log.e("URL", connection.getURL().toString());
Log.e("JSONObject", register.toString());
} catch (Exception e) {
Log.e("MAIN Exception", e.toString());
}
try {
int statuscode = connection.getResponseCode();
if (statuscode == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();
} else {
}
} catch (IOException e) {
Log.e("IOException", e.toString());
}
try {
rd = new BufferedReader(new InputStreamReader(is));
response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\n');
}
Log.e("Response", response.toString() + " ");
rd.close();
} catch (IOException e) {
Log.e("BUFFER_READER", e.toString());
} catch (NullPointerException e) {
Log.e("NullPointerException", e.toString());
} finally {
connection.disconnect();
}
try {
return new JSONObject(response.toString());
} catch (JSONException e) {
Log.e("JSONException", e.toString());
}
return null;
}
Also You are using localHost you must have emulator which can connect to localhost. Unless it will not going to work on any device.
这篇关于Android的HttpURLConnection类用JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!