我想将数据发布到url
并获取Null pointer exception
我的JSON
URL
包含
{
"Details":
[
{
"Status":"NO UPDATES"
}
]
}
我收到了错误行:
String status = object.getString("Status").trim(); //error Line
完整代码:
btnPost = (Button)findViewById(R.id.btnPost);
btnPost.setOnClickListener(this);
btnPost.setOnClickListener(new OnClickListener()
{
@SuppressWarnings("null")
@Override
public void onClick(View arg0)
{
try
{
String postReceiverUrl = "http://";
Log.v(TAG, "postURL: " + postReceiverUrl);
// HttpClient
@SuppressWarnings("resource")
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
jsonobject.put("IDNo", IDNo.getText().toString());
jsonobject.put("Position", Position.getText().toString());
jsonobject.put("Data", Data.getText().toString());
HttpResponse response = httpClient.execute(httpPost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
Log.v("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(LoginPage.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
if(status.toString().equals("FAILED"))
{
Toast.makeText(getBaseContext(), "Wrong Credentials",100).show();
}
else
{
Toast.makeText(getBaseContext(), "Details Inserted",100).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
答案 0 :(得分:1)
正确解析 JSONObject object = new JSONObject(jsonResult);
JSONArray array=object.getJsonarray("Details");
for(int i=0;i<array.length();i++)
{
JSONObject innerObject=array.getJsonObject(i);
String s= innerObject.getString("Status");
}
,你会看到你的回复: -
JSON
首先尝试制作 { "Details":[{"Status":"NO UPDATES"}]}
的对象而不是JSONObject
之后的对象,请看下面的示例: -
JSONArray
答案 1 :(得分:0)
Modift String status = object.getString("Status").trim();
到
String status = object.get("Details").getAsJsonArray()[0].getString("Status").trim();
答案 2 :(得分:0)
您正在使用getString("Status")
,如果密钥在JSON中不可用,则可能会返回null
。我建议你使用optString("Status")
如果密钥不可用,它将返回空白字符串。
JSONObject jsonObject = new JSONObject(stringJSON);
JSONArray jsonArray = jsonObject.optJSONArray("Details");
if(jsonArray != null){
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jObject = jsonArray.optJSONObject(i);
if(jObject != null){
String strStatus = jObject.optString("Status");
}
}
}
答案 3 :(得分:0)
试用此代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div>abc</div>
<button type="button" id="zoom-in">zoom in</button>
<button type="button" id="zoom-out">zoom out</button>
<script>
$('#zoom-in').click(function() {
updateZoom(0.1);
});
$('#zoom-out').click(function() {
updateZoom(-0.1);
});
zoomLevel = 1;
var updateZoom = function(zoom) {
zoomLevel += zoom;
$('body').css({
zoom: zoomLevel,
'-moz-transform': 'scale(' + zoomLevel + ')'
});
}
</script>
答案 4 :(得分:0)
你的代码应该是这样的
if j