简体   繁体   English

Volley解析错误org.json.JSONException:输入的结束字符为0

[英]Volley parse Error org.json.JSONException: End of input at character 0 of

I;m developing a android app and java Jax rs Restful web service. 我正在开发一个Android应用程序和Java Jax rs Restful Web服务。 When my app sending json on service that error occure. 当我的应用在服务上发送json时,发生此错误。 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. 如果我从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;
    }
}

}

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

You are getting a blank / empty response. 您得到一个空白/空的响应。 You are not getting exception but the json encoded text is empty. 您没有收到异常,但json编码的文本为空。

Check for Post Request / Get request - what you are using in the server side. 检查发布请求/获取请求-您在服务器端使用的内容。

Check whether the API returns the valid json by debugging. 通过调试检查API是否返回有效的json。

Check that the response should be a valid json. 检查响应是否为有效的json。 you can check whether the json is valid or not in the below link. 您可以在以下链接中检查json是否有效。 Json Validator 杰森验证器

Check whether you added the below permission to the Android Manifest. 检查是否向Android清单中添加了以下权限。

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

Check whether you have working internet connection. 检查您的互联网连接是否正常。

Reference Links : 参考链接:

Link 1 , Link 2 , Link 3 链接1链接2链接3

Tutorial 教程

Happpy Coding..!! 开心的编码.. !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM