I;m developing a android app and java Jax rs Restful web service. When my app sending json on service that error occure. And if I send json from Restful Client browser Service Successfully receive data but cannot receive from android app.... my app and service code is here.This is my android Activity.
public class feedback extends SameCodeClass implements View.OnClickListener {
String f_name, f_mobile, f_description, f_ratVel;
double f_rating;
RequestQueue requestQueue;
String responseServer;
feedbackModel fbModel;
private String TAG = feedback.class.getSimpleName();
private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";
private ProgressDialog pDialog;
EditText name, mobile, des;
TextView rating;
RatingBar ratingBar;
Button submint_fb;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.feedback);
Initialize();
addListenerOnRatingBar();
}
protected void Initialize() {
name = (EditText) findViewById(R.id.name);
mobile = (EditText) findViewById(R.id.mobile);
des = (EditText) findViewById(R.id.des);
rating = (TextView) findViewById(R.id.rating);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
submint_fb = (Button) findViewById(R.id.fsbutton);
submint_fb.setOnClickListener(this);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
requestQueue = Volley.newRequestQueue(this);
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}
protected void setValues() {
f_name = name.getText().toString();
f_mobile = mobile.getText().toString();
f_description = des.getText().toString();
f_ratVel = rating.getText().toString();
f_rating = Double.parseDouble(f_ratVel);
if (f_name != null && f_mobile != null && f_description != null && f_rating != 0.0) {
// Toast.makeText(getBaseContext(),f_name+","+f_mobile+","+f_description+","+f_rating,Toast.LENGTH_LONG).show();
createJsonObj();
} else {
if (f_name.isEmpty()) {
Toast.makeText(getBaseContext(), "Enter Name First.", Toast.LENGTH_LONG).show();
} else if (f_mobile.isEmpty()) {
Toast.makeText(getBaseContext(), "Enter Mobile Number.", Toast.LENGTH_LONG).show();
} else if (f_description.isEmpty()) {
Toast.makeText(getBaseContext(), "Enter Your Message.", Toast.LENGTH_LONG).show();
} else if (f_ratVel.isEmpty()) {
Toast.makeText(getBaseContext(), "0.0 Rating Not Acceptable.", Toast.LENGTH_LONG).show();
}
}
}
// Volley Json Request...
private void createJsonObj() {
String url = "http://192.168.23.1:8080/RESTfulExample/rest/file/feedback";
showProgressDialog();
Map<String, String> jsonParams = new HashMap<String, String>();
//jsonParams.put("param1", youParameter);
jsonParams.put("name", f_name);
jsonParams.put("mobile", f_mobile);
jsonParams.put("description", f_description);
jsonParams.put("rating", f_ratVel);
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// verificationSuccess(response);
Toast.makeText(getBaseContext(), response.toString(), Toast.LENGTH_LONG).show();
hideProgressDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// verificationFailed(error);
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Volley", "Error. HTTP Status Code:" + networkResponse.statusCode);
}
if (error instanceof TimeoutError) {
Log.e("Volley", "TimeoutError");
} else if (error instanceof NoConnectionError) {
Log.e("Volley", "NoConnectionError");
} else if (error instanceof AuthFailureError) {
Log.e("Volley", "AuthFailureError");
} else if (error instanceof ServerError) {
Log.e("Volley", "ServerError");
} else if (error instanceof NetworkError) {
Log.e("Volley", "NetworkError");
} else if (error instanceof ParseError) {
Log.e("Volley", "Parse Error: "+ error.getMessage());
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
// headers.put("User-agent", "My useragent");
return headers;
}
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
requestQueue.add(myRequest);
}
public void addListenerOnRatingBar() {
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
rating = (TextView) findViewById(R.id.rating);
//if rating value is changed,
//display the current rating value in the result (textview) automatically
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rat,
boolean fromUser) {
rating.setText(String.valueOf(rat));
}
});
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.fsbutton: {
if(isOnline()){
setValues();
// requestData("http://192.168.23.1:8080/RESTfulExample/rest/file/show");
}else{
Toast.makeText(this,"Network isn't available", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id != R.id.fdb) {
DealMenu(id);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
BackButtonHandle();
return;
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
}
Now my service method that receive the that of this class.
@POST
@Path("/feedback")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public void postFeedback(newFeedModel fbkModel){
String name, mobile,description;
int rating;
f_name = fbkModel.getName();
f_mobile = fbkModel.getMobile().toString();
f_description = fbkModel.getDescription();
f_rating = fbkModel.getRating();
dataService.insertFeedback(f_name, f_mobile, f_description, f_rating);
}
And my feedback Model is here,...
package com.live.rest.Model;
import java.io.Serializable;
public class newFeedModel implements Serializable{
public String id;
public String name;
public String mobile;
public String description;
public String rating;
public void setId(String id) {
this.id = id;
}
public newFeedModel() {
super();
}
public newFeedModel(String name, String mobile, String description, String rating) {
super();
this.name = name;
this.mobile = mobile;
this.description = description;
this.rating = rating;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
}