java.lang.NullPointerException when POST with AsyncHttpClient - java

I have an endpoint /test which expects Map :
#POST("/hello")
#PermitAll
public JSONObject test(Map param) throws JsonParseException {
String elementName = param.get("name").toString();
String elem = param.get("elem").toString();
JSONObject json=new JSONObject();
try {
json.put("id",1);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
And I'm sending asynchronous POST (postin JSON) using AsyncHttpClient:
public static void asyncCallPost(JSONObject jsonData) throws Exception {
AsyncHttpClient client = new AsyncHttpClient();
try {
Response response = client.preparePost(url)
.addHeader("Content-Type", "application/json")
.addHeader("Content-Length", "" + jsonData.length())
.setBody(jsonData.toString()).execute().get();
if (response.getStatusCode() != 200) {
throw new Exception("Error ");
}
} finally {
client.close();
}
}
But I am getting java.lang.NullPointerException.
Is it because I don't pass any Map to /hello? If so how make POST with jsonData as Map to the endpoint?

I cannot tell without further information however I suspect that the following needs to change;
String elementName = param.get("name").toString();
String elem = param.get("elem").toString();
to
String elementName;
if(param.get("name") != null){
elementName = param.get("name").toString();
}
String elem;
if(param.get("elem") != null){
elem = param.get("elem").toString();
}
The reason being the param.get("name") is returning a null which you are attempting to run toString on, hence the null pointer exception.

You're setting a JSON string to the body, that's why the Map is null or empty or full of garbage (I didn't check myself).
The Map is a representation of the key-value pairs that are POSTed. To have the Map non-empty, you would have to post e.g. name=myname&elem=myelement as type application/x-www-form-urlencoded.

Related

Java "string" is not a JSONObject error when I try to parse string to JSON [duplicate]

I am now currently using a weather API from http://wiki.swarma.net/index.php?title=%E5%BD%A9%E4%BA%91%E5%A4%A9%E6%B0%94API/v2 and wished to convert the JSONObject into printable Strings. However, when I am working on the following code, two errors occurred:
public class getApi {
private static final String WEATHER_MAP_URL = "https://api.caiyunapp.com/v2/TAkhjf8d1nlSlspN/121.6544,25.1552/realtime.json";
private static final String WEATHER_TEST_API = "TAkhjf8d1nlSlspN";
public static JSONObject getWeatherJson() {
try {
URL url = new URL( WEATHER_MAP_URL );
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.addRequestProperty( "x-api-key", WEATHER_TEST_API );
BufferedReader reader = new BufferedReader(
new InputStreamReader( connection.getInputStream()) );
StringBuffer json = new StringBuffer( 1024 );
String tmp;
while( (tmp = reader.readLine()) != null )
json.append(tmp).append("\n");
reader.close();
JSONObject data = new JSONObject( json.toString() );
if(data.getJSONObject("status").toString() != "ok" ) {
return null;
}
return data;
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public static void main( String[] args ) {
JSONObject WeatherJson = getWeatherJson();
try {
JSONArray details = WeatherJson.getJSONObject("result").getJSONObject("hourly").
getJSONArray("skycon");
System.out.println(details.getJSONObject(0).getJSONObject("value").toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The JSONObject structure, which is also shown in the link above, is like this:
{
"status":"ok",
"lang":"zh_CN",
"server_time":1443418212,
"tzshift":28800,
"location":[
25.1552, //latitude
121.6544 //longitude
],
"unit":"metric",
"result":{
"status":"ok",
"hourly":{
"status":"ok",
"skycon":[
{
"value":"Rain",
"datetime":"2015-09-28 13:00"
},
{
...
}]
}
}
}
The error occurred:
org.json.JSONException: JSONObject["status"] is not a JSONObject.
at org.json.JSONObject.getJSONObject(JSONObject.java:557)
at getApi.getWeatherJson(getApi.java:34)
at getApi.main(getApi.java:45)
Exception in thread "main" java.lang.NullPointerException
at getApi.main(getApi.java:47)
I have looked at similar posts on the topic is not a JSONObject Exception but found that none of them can help me. I suspect that something is wrong with requesting the data, so actually, getWeatherJson() returns a null object and results in the NullPointerException and JSONObjectException.
Can anyone help me with the code?
According to the getJSONObject() Javadoc, this method will throw an exception if the returned object isn't a true JSON object, which it isn't because "status" is a string. As such, try using data.getString("status").
The status field in the JSON document you have posted is not an object. In JSON, objects are enclosed in with {} brackets. The result node however, is a nested object which holds the status key/value pair. Try the following:
JSONObject data = new JSONObject(json.toString());
if(data.getJSONObject("result").get("status").toString() != "ok" ) {
return null;
}

How to receive form parameters values in server?

i have React JS application where i send post request to server with form submission using axios library.
client request:
sendData(data,price) {
axios.post('http://localhost:8080/SampleJavaAPP/UserServer', {
item: data,//these value
price:price//these value
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
i am not sure how to get these values into server i am doing in server for getting value like this
String name = request.getParameter("item");
String price = request.getParameter("price");
System.out.println("Result "+name + price);
But it gives null values in server. how to receive these values parameters in server?
As Axios is sending Json data you will not be able to read its direct.
There are 2 possible solutions:
Either send data as form-data.
Read & Parse JSON at servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
jb.append(line);
}
} catch (Exception e) { /*report an error*/ }
try {
JSONObject jsonObject = HTTP.toJSONObject(jb.toString());
String price = jsonObject.get("price"); // will return price value.
} catch (JSONException e) {
throw new IOException("Error parsing JSON request string");
}
}
Request Body is not retrieved by request.getParameter(). You need to retrieve it by request.getReader().
String body = IOUtils.toString(request.getReader());
It is suggested to use Apache Commons IO to get Content first. As your request is in JSON format. You can use Jackson to convert the String into Map.
Map<String, String> map = mapper.readValue(body, new TypeReference<Map<String, String>>(){});
System.out.println(map.get("item"));
System.out.println(map.get("price"));
request.getParameter()is referring to URL parameters ->myurl?someparameter=1
By doing request.getParameter("item"), your URL would need to look like http://localhost:8080/SampleJavaAPP/UserServer?item=myitem
What you are actually doing here
sendData(data,price) {
axios.post('http://localhost:8080/SampleJavaAPP/UserServer', {
item: data,//these value
price:price//these value
}
is adding the objects to the request body, which is IMHO correct. There for you won't find any parameters item or price on your server side request object.
What you need to do, is to parse the requests body. With request.getInputStream() you can get the Inputstream. I suggest you use an object mapper which makes that really easy. See Intro to the Jackson ObjectMapper
In your servlet, you do something like this:
ObjectMapper objectMapper = new ObjectMapper();
MyItem myItem = objectMapper.readValue(request.getInputStream(), MyItem.class);
public class MyItem{
String price;
String item;
public void setItem(String item) {
this.item = item;
}
public void setPrice(String price) {
this.price = price;
}
public String getItem() {
return item;
}
public String getPrice() {
return price;
}
}

java.io.EOFException: End of input at line 1 column 1 path $ in Gson parser

I'm parsing a JSON string by using Gson and Retrofit. I have this JSON string:
{"message":["Email has already been taken"]}
I get the below exception still and don't know why:
java.io.EOFException: End of input at line 1 column 1 path $
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:260)
at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:273)
People who know how to get the value of message field please help me.
BaseApiDto.java
public class BaseApiDto {
#SerializedName("message")
public String[] message;
public String getError() {
return message[0];
}
}
HandErrorUtils.java
public static void handleError(FragmentActivity activity, Throwable e) {
String msg = null;
if(e instanceof HttpException){
// Error message in json
Gson gson = new Gson();
TypeAdapter<BaseApiDto> adapter = gson.getAdapter(BaseApiDto.class);
ResponseBody body = ((HttpException) e).response().errorBody();
// Status code
HttpException httpException = (HttpException) e;
int statusCode = httpException.code();
if (statusCode == 500) {
showErrorDialog(activity, activity.getString(R.string.dialog_msg_error_401), true);
} else if (statusCode == 401) {
showErrorDialog(activity, activity.getString(R.string.dialog_msg_error_401), true);
} else {
try {
Timber.w("body.string() " + body.string());
// TODO : EXCEPTION HAPPEN IN HERE
BaseApiDto errorDto = adapter.fromJson(body.string());
msg = errorDto.getError();
Timber.w("msg " + msg);
} catch (Exception ex) {
// TODO : EXCEPTION HAPPEN IN HERE
ex.printStackTrace();
}
showErrorDialog(activity, msg, false);
}
}
}
UPDATE I assign body.toString() to variable, somehow it worked.
String response = body.string();
BaseApiDto errorDto = adapter.fromJson(response);
It worked because I didn't call body.string() twice.
I assign body.toString() to variable, somehow it worked.
String response = body.string();
BaseApiDto errorDto = adapter.fromJson(response);

Is this Java Json (Gson) code to check correct response is not too redundant?

I have Json parser class which was written by someone else. I smell that it is something bad with this checker method:
public boolean isCorrectResponse() {
try {
if (localResponse != null) {
JSONObject jResponse = new JSONObject(localResponse);
if (jResponse.get("result") instanceof JSONObject) {
JSONObject jResult = jResponse.getJSONObject("result");
if (jResult.get("error") instanceof JSONArray) {
JSONObject jError = jResult.getJSONArray("error").getJSONObject(0);
if (!jError.toString().equals("")) {
String errorMsg = jError.getString("msg");
String errorCode = jError.getString("code");
showErrorMessage(errorCode + "; " + errorMsg);
return false;
}
}
}
} else {
return false;
}
} catch (JSONException e) {
L.e("ERROR: on isCorrectResponse method!");
e.printStackTrace();
//return false; //Added myself Google Json should throw error shouldn't it??? Which means response was wrong???
}
return true;
}
Shouldn't error be thrown in first attempt to create
JSONObject jResponse = new JSONObject(localResponse);
and everything would be solved instantly (I need only to return false)? Is these additional checks in try body are needed at all?
I'm using Google Gson library to parse Json and developing for Android.
Parse each tag of the JSON is a high work and it's not a very good idea check if (jResponse.get("result") instanceof JSONObject)
Better use
JSONObject result =jResponse.get("result") instanceof JSONObject)
if (result == null) return false;
Anyway if you add a catch to NullPointerException you can avoid this if. When the result is null, when you try to parse the next tag the app will throw an exception

Java sends a JSON string in POST request with escaped double quotes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I stop HTTP from escaping quotes?
I am creating a JSONObject and sending JSON string to a server in a POST request body.
public String toJson() {
JSONObject filter = new JSONObject();
try {
filter.put("gender", gender.getCode());
filter.put("feature_id", productCategory);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject filterObject = new JSONObject();
try {
filterObject.put("filter", filter);
} catch (JSONException e) {
e.printStackTrace();
}
return filterObject.toString();
}
So I'm creating a request:
private IJsonExecutorInterface requestExecutorForRelativePathAndParams(String path, WebParams params) throws UnsupportedEncodingException {
HttpPost postRequest = new HttpPost(rootUrl + path);
if(params != null) {
postRequest.setHeader("content-type", params.getContentType());
postRequest.setEntity(params.getFormEntity());
}
// Blah blah
return executor;
}
public IJsonExecutorInterface getProducts(ProductFilter filter, int offset, int limit) throws UnsupportedEncodingException {
WebParams webParams = new WebParams();
webParams.addPair("filter", filter.toJson());
webParams.addPair("offset", String.format("%d", offset));
webParams.addPair("limit", String.format("%d", limit));
return requestExecutorForRelativePathAndParams("products", webParams);
}
// WebParams class
public class WebParams {
private ArrayList<NameValuePair> params;
private String contentType = "application/x-www-form-urlencoded";
public WebParams() {
params = new ArrayList<NameValuePair>();
}
public void addPair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
public String getContentType() {
return contentType;
}
public HttpEntity getFormEntity() throws UnsupportedEncodingException {
return new UrlEncodedFormEntity(params);
}
}
I see it in debugger: it's ok.
But on my server I getting something like this:
Array
(
[filter] => {\"gender\":\"w\",\"feature_id\":\"41_7459\"}
[offset] => 0
[limit] => 18
)
The quotes ARE escaped.
I don't want to replace something on the server. replace("\\\"", "\"") in Java doesn't affect on the string.
Looks like your using a UrlEncodedFormEntity, which, according to the docs is 'An entity composed of a list of url-encoded pairs' ([http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html]). I've never used this, but it doesn't sound like its what you want, as you are sending data in the post body, not through URL parameters.
I've used the StringEntity class before to send json data via post, although it only encodes a string, not name/value pairs, so you've got to do a bit more work to put the string in a format you want to deal with on your server:
public class WebParams {
private ArrayList<NameValuePair> params;
private String contentType = "application/x-www-form-urlencoded";
public WebParams() {
params = new ArrayList<NameValuePair>();
}
public void addPair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
public String getContentType() {
return contentType;
}
public HttpEntity getFormEntity() throws UnsupportedEncodingException {
//TODO: Build a string in what ever format you want.
// This will include the gender & feature_id fields as well as the json
StringBuilder b = new StringBuilder();
for(NameValuePair nvp : params) {
builder.append(nvp.getName()).append('=').append(nvp.getValue()).append(',');
}
//Now that we have a string to send to the server, get your entity!
StringEntity entity = new StringEntity(b.toString());
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
return entity;
}
}
Is there a problem to use simple quotes instead of double quotes? Because I think it would solve your problem.

Categories

Resources