使用json android将数据上载到服务器
我需要在android应用程序中使用json将数据上传到服务器。 这是为上传评论提供的详细信息使用json android将数据上载到服务器,android,json,Android,Json,我需要在android应用程序中使用json将数据上传到服务器。 这是为上传评论提供的详细信息 ------WebKitFormBoundaryZEhUiG82B2leVEc9 Content-Disposition: form-data; name="Comment" Testing update 这是我用来更新的代码 URL url = new URL("MY URL comes here"); // here is your URL path String js = "Content
------WebKitFormBoundaryZEhUiG82B2leVEc9
Content-Disposition: form-data; name="Comment"
Testing update
这是我用来更新的代码
URL url = new URL("MY URL comes here"); // here is your URL path
String js = "Content-Disposition: form-data; name=Comment";
String id = "ID=1"; //need to mention the id for upload
String json = "testttting update";
JSONObject postDataParams = new JSONObject();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
OutputStream os = conn.getOutputStream();
os.write(js.getBytes());
os.write(id.getBytes());
os.write(json.getBytes());
os.flush();
os.close();
此请求将没有响应。只有它会更新到服务器。
请推荐我。我尝试了很多,但没有找到解决方案。尝试此方法来呼叫post请求
public class SendPostRequest extends AsyncTask<Void, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(Void... arg0) {
try {
URL url = new URL("MY URL comes here"); // here is your URL path
JSONObject postDataParams = new JSONObject();
postDataParams.put("ID", "1");
postDataParams.put("Name", "Chetan");
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(60000 /* milliseconds */);
conn.setConnectTimeout(60000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new
InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
/*In result you will get your response*/
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
公共类SendPostRequest扩展了异步任务{
受保护的void onPreExecute(){}
受保护的字符串doInBackground(无效…arg0){
试一试{
URL=新URL(“我的URL来了”);//这是您的URL路径
JSONObject postDataParams=新的JSONObject();
postDataParams.put(“ID”,“1”);
postDataParams.put(“名称”、“棋盘”);
Log.e(“params”,postDataParams.toString());
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(60000/*毫秒*/);
conn.setConnectTimeout(60000/*毫秒*/);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
BufferedReader in=新的BufferedReader(新的
输入流阅读器(
conn.getInputStream());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
而((line=in.readLine())!=null){
某人附加(行);
打破
}
in.close();
使某人返回字符串();
}
否则{
返回新字符串(“false:+responseCode”);
}
}
捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
/*结果你会得到你的回应*/
Toast.makeText(getApplicationContext(),result,
Toast.LENGTH_LONG).show();
}
}
公共字符串getPostDataString(JSONObject参数)引发异常{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
迭代器itr=params.keys();
while(itr.hasNext()){
String key=itr.next();
对象值=params.get(键);
如果(第一)
第一个=假;
其他的
结果。追加(&);
result.append(URLEncoder.encode(键,“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(value.toString(),“UTF-8”);
}
返回result.toString();
}
用截击怎么样?我现在才用截击。你能告诉代码如果你使用截击,代码会有怎样的变化吗?----WebKitFormBoundaryZEhUiG82B2leVEc9
。对你的边界在哪里?@greenapps-你能解释一下如何添加这个吗?你可以用谷歌搜索一下。Volley是最流行的与服务器进行HTTP连接的库。您可以在@paul找到一些教程和链接