Java 无法使用json将数据保存到url
我的json代码{“状态”:“无更新”} 我正在获取输出,因为无法保存数据。 当我检查log.v(JSONResult,“JSONResult”)时,我得到的结果是“失败” 我无法解决这个问题 请帮帮我Java 无法使用json将数据保存到url,java,android,sql,json,Java,Android,Sql,Json,我的json代码{“状态”:“无更新”} 我正在获取输出,因为无法保存数据。 当我检查log.v(JSONResult,“JSONResult”)时,我得到的结果是“失败” 我无法解决这个问题 请帮帮我 btn_Save.setOnClickListener(new OnClickListener() { public void onClick(View v) {try {StrictMode.ThreadPolicy po
btn_Save.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{try
{StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String postReceiverUrl = "http://";
Log.v(TAG, "postURL: " + postReceiverUrl);
// HttpClient
@SuppressWarnings("resource")
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
Log.v(TAG, jsonobject.toString());
nameValuePairs.add(new BasicNameValuePair("EmployeeId", wepemployeeDeptId.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("visitorname", wepName.getText().toString()));
Log.v(TAG, "Visitor Name: " + wepName.getText().toString());
nameValuePairs.add(new BasicNameValuePair("PhoneNo", wepMobile.getText().toString()));
// nameValuePairs.add(new BasicNameValuePair("WhomToMeet", wepWhomToMeet.getSelectedItem().toString()));
nameValuePairs.add(new BasicNameValuePair("Address", wepAddress.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("EmailID", wepEmail.getText().toString()));
// nameValuePairs.add(new BasicNameValuePair("visitType", wepVisitType.getSelectedItem().toString()));
nameValuePairs.add(new BasicNameValuePair("PurposeOfMeet", wepPurpose.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("DateofVisit", wepVisitDate.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Intime", wepInTime.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("Outtime", wepOutTime.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("BadgeNo", wepBadgeNumber.getText().toString()));
// nameValuePairs.add(new BasicNameValuePair("CreatedBy", wepcreatedby.getText().toString()));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
Log.i("jsonResult",jsonResult);
JSONObject object = new JSONObject(jsonResult);
String status = object.getString("Status").trim();
Toast.makeText(getBaseContext(), "Please wait...",100).show();
if(status.toString().equals("SUCCESS"))
{
Intent i = new Intent(Entry.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Toast.makeText(getBaseContext(), "Details Inserted",1000000).show();
}
if(status.toString().equals("FAILED"))
{
Toast.makeText(getBaseContext(), "Couldn't save the data...",1000000).show();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
btn\u Save.setOnClickListener(新的OnClickListener()
{public void onClick(视图v)
{试试看
{StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
字符串postReceiverUrl=“http://”;
Log.v(标签“postURL:+postReceiverUrl”);
//HttpClient
@抑制警告(“资源”)
HttpClient HttpClient=新的DefaultHttpClient();
//柱头
HttpPost HttpPost=新的HttpPost(postReceiverUrl);
List nameValuePairs=新的ArrayList(2);
Log.v(标记,jsonobject.toString());
添加(新的BasicNameValuePair(“EmployeeId”,WEPEmployedeptid.getText().toString());
添加(新的BasicNameValuePair(“visitorname”,wepName.getText().toString());
Log.v(标记“访问者名称:”+wepName.getText().toString());
添加(新的BasicNameValuePair(“PhoneNo”,wepMobile.getText().toString());
//添加(新的BasicNameValuePair(“WhomToMeet”,WepHomToMeet.getSelectedItem().toString());
添加(新的BasicNameValuePair(“地址”,wepAddress.getText().toString());
添加(新的BasicNameValuePair(“EmailID”,wepEmail.getText().toString());
//添加(新的BasicNameValuePair(“visitType”,wepVisitType.getSelectedItem().toString());
添加(新的BasicNameValuePair(“会议目的”,wepppurpose.getText().toString());
添加(新的BasicNameValuePair(“DateofVisit”,wepVisitDate.getText().toString());
添加(新的BasicNameValuePair(“Intime”,wepInTime.getText().toString());
添加(新的BasicNameValuePair(“Outtime”,wepOutTime.getText().toString());
添加(新的BasicNameValuePair(“BadgeNo”,wepBadgeNumber.getText().toString());
//添加(新的BasicNameValuePair(“CreatedBy”,wepcCreatedBy.getText().toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP post请求
HttpResponse response=httpClient.execute(httpPost);
字符串jsonResult=inputStreamToString(response.getEntity().getContent()).toString();
Log.i(“jsonResult”,jsonResult);
JSONObject对象=新的JSONObject(jsonResult);
String status=object.getString(“status”).trim();
Toast.makeText(getBaseContext(),“请稍候…”,100.show();
if(status.toString().equals(“SUCCESS”))
{
意图i=新意图(Entry.this,MainActivity.class);
i、 设置标志(意图、标志、活动、清除、顶部);
星触觉(i);
Toast.makeText(getBaseContext(),“插入的详细信息”,1000000).show();
}
if(status.toString().equals(“FAILED”))
{
Toast.makeText(getBaseContext(),“无法保存数据…”,1000000.show();
}
}
捕获(JSONException e)
{
e、 printStackTrace();
}
捕获(客户端协议例外e)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
});
//根据您的要求添加密钥
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader reader = null;
// Send data
try {
// Defined URL where to send data
URL url = new URL("Your URL");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();EmployeeId
谢谢你的回复。但我希望状态对象出现在我的代码中,这样当它转到url时,我将得到相应的响应。iam得到的错误是关于JSONObject对象=新JSONObject(jsonResult);String status=object.getString(“status”).trim()@Pankaj KumarIn我的代码我也在阅读回复。再次阅读代码。你也应该像我一样使用URLConnection。由于http post是depricated.org.json.JSONException:Value,我在JSONObject object=newjsonobject(jsonResult)上得到这个错误;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader reader = null;
// Send data
try {
// Defined URL where to send data
URL url = new URL("Your URL");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();EmployeeId