Android String.format()没有任何理由崩溃

时间:2018-01-13 15:27:13

标签: java android string format android-volley

我发生了一个奇怪的崩溃/错误,没有改变任何东西。 我正在使用Volley进行网络请求,我有一个单独的类,我的URL端点。

我使用MY_REQUEST_URL + myparam=%s作为链接,在排球请求中我正在使用String.format(EndPoints.MY_REQUEST_URL, myparameter),当我在我的应用中运行此请求时,它会崩溃(请参阅下面的日志)。

更奇怪的是,我在同一个应用程序中使用相同类型的请求,除了这个和另一个,它们都可以正常工作。

MyClass代码:

public class MyClass extends Fragment{
// other declarations
ArrayList<JSONObject> data;
View view 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_myclass, null);

    // initializing lots of views
    data = new ArrayList<JSONObject>();
    String userid = Utils.getStringFromPreferences(getActivity(), Utils.VAR_USERID); // this is a custom method of getting from shared preferences, it is static in my custom Utils class
    getData(userid);

    return view;
  }

  //different methods

  //getData Method
   private void getData(String userid) {
    String url = String.format(EndPoints.URL_MY_REQUEST_URL, userid);
    Log.d("request url debug", url);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Result handling

                    Log.e("data", response);
                    if (!response.equals("") || response != null) {
                        try {
                            // response handling


                        } catch (JSONException e) {
                            // e.printStackTrace();

                        }
                    } else {
                    }
                }

            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("Something went wrong!");
            error.printStackTrace();

        }
    });
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            10000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Add the request to the queue - i have a custom class for this, used in every network request that works fine 
    VolleyRequestQue.getInstance(InsuitBusiness.getContext()).addToRequestQueue(stringRequest);
} 

}

这是Endpoints类:

public class Endpoints {
public static String MY_MAIN_URL = "https://www.myweb.com/"

public static String MY_REQUEST_URL = MY_MAIN_URL + "scripts/getData.php?userid=%s"
// the rest of the class is all the same like stringname = MY_MAIN_URL + "phpscript.php?parameter=%s"

}

这是错误日志:

 Caused by: java.util.MissingFormatArgumentException: Format specifier '%s'
            at java.util.Formatter.format(Formatter.java:2490)
            at java.util.Formatter.format(Formatter.java:2426)
            at java.lang.String.format(String.java:2626)
            at com.myapp.myapp.MyClass.getData(MyClass.java:1220)
            at com.myapp.myapp.MyClass$getDataAsync.doInBackground(MyClass.java:1208)
            at com.myapp.myapp.MyClass$getDataAsync.doInBackground(MyClass.java:1204)
            at android.os.AsyncTask$2.call(AsyncTask.java:304)

1 个答案:

答案 0 :(得分:0)

MY_MAIN_URL应该具有以下价值:&#34; https://www.myweb.com/&#34;我错误地给了另一个含有&#34;%s&#34;这就是我得到异常和崩溃的方式。

对于遇到同样问题的未来人员,请确保没有更多&#34;%s&#34;在链接中,记录调试器中的链接,看看它是如何显示的。

感谢大家的时间。