簡體   English   中英

Volley解析錯誤org.json.JSONException:輸入的結束字符為0

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

我正在開發一個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;
}
}

您得到一個空白/空的響應。 您沒有收到異常,但json編碼的文本為空。

檢查發布請求/獲取請求-您在服務器端使用的內容。

通過調試檢查API是否返回有效的json。

檢查響應是否為有效的json。 您可以在以下鏈接中檢查json是否有效。 傑森驗證器

檢查是否向Android清單中添加了以下權限。

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

檢查您的互聯網連接是否正常。

參考鏈接:

鏈接1鏈接2鏈接3

教程

開心的編碼.. !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM