Volley parse错误org.json.JSONException:字符0处的输入结束

时间:2015-12-23 11:24:06

标签: java android android-intent jax-rs jax-ws

我正在开发一个Android应用程序和java Jax rs Restful Web服务。当我的应用程序发送json服务时发生错误。如果我从Restful Client浏览器发送json服务成功接收数据但无法从Android应用程序接收.... 我的应用和服务代码在这里。这是我的Android活动。

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;
    }
}

}

现在我的服务方法接收了这个类的服务方法。

@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);
}

我的反馈模型就在这里,......

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;
}
}

1 个答案:

答案 0 :(得分:1)

您收到空白/空白回复。你没有得到异常,但json编码的文本是空的。

检查发布请求/获取请求 - 您在服务器端使用的内容。

检查API是否通过调试返回有效的json。

检查响应是否应该是有效的json。你可以在下面的链接中检查json是否有效。 Json Validator

检查您是否在Android Manifest中添加了以下权限。

<uses-permission android:name = "android.permission.INTERNET"/>

检查您是否有互联网连接。

参考链接:

Link 1Link 2Link 3

Tutorial

Happpy Coding .. !!