API 在 phone 当地语言是阿拉伯语时调用 android

API calling when phone local language is Arabic in android

我正在做一个 android 项目,当移动本地语言是阿拉伯语时,我必须用英语呼叫 APIs。我听不懂英文 API URl String。 我的API调用文件(PackageapiCall.class)如下:

包 com.example.app.roamer.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.app.roamer.Constant;
import com.example.app.roamer.R;
import com.example.app.roamer.activities.SplashScreen;
import com.example.app.roamer.app.AppController;
import com.example.app.roamer.fragments.AlltripsFragment;
import com.example.app.roamer.helper.DateFormatsHelper;
import com.example.app.roamer.fragments.TaTripsFragment;
import com.example.app.roamer.models.AllTrips;
import com.example.app.roamer.tasks.GenerateTokenOperation;
import com.example.app.roamer.tasks.ParsingOperation;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static com.example.app.roamer.activities.HomeActivity.TAG;

/**
 * Created by manish on 22-12-2016.
 */

public class PackageApicalls {
    private Request.Priority priority = Request.Priority.HIGH;
    Context ctx;
    private List<AllTrips.TripsBean> allTripsByMobileUserIDNewData, allTripsByMobileUserIDNewData1;
    AlltripsFragment alltripsFragment;
    TaTripsFragment taTripsFragment;
    Boolean splashScreenApiCall=false;
     SharedPreferences sharedPreferences;
    private int mobileUserId;

    public PackageApicalls(Context ctx, AlltripsFragment alltripsFragment) {
        this.ctx=ctx;
        this.alltripsFragment=alltripsFragment;
    }
    public PackageApicalls(Context ctx, TaTripsFragment taTripsFragment) {
        this.ctx=ctx;
        this.taTripsFragment=taTripsFragment;
    }
    public PackageApicalls(Context ctx) {
        this.ctx=ctx;
    }


    public void getalltripdetails() {

     //  String gettodaysdate="02-02-2016";
        DateFormatsHelper dateFormatsHelper = new DateFormatsHelper();
        String gettodaysdate = dateFormatsHelper.gettodaysdate();
        sharedPreferences = ctx.getSharedPreferences(Constant.MyPREFERENCES, Context.MODE_PRIVATE);
         mobileUserId = sharedPreferences.getInt("mobileUserId", 0);

       //mobileUserId=1;
        String url = ctx.getResources().getString(R.string.alltrips_api_url,mobileUserId,gettodaysdate);
        Log.d(TAG, "manish-url-packageapicalls"+url);
        StringRequest postRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (Looper.myLooper() == Looper.getMainLooper()) {
                            Log.d(TAG, "main thread-true");
                        } else {
                            Log.d(TAG, "main thread-false");
                        }
                        // response
                        Log.d("Response", response);

                        allTripsByMobileUserIDNewData = parsejson(response);
//                          setupui(allTripsByMobileUserIDNewData);
                        Log.d("manishbutola", "onResponse: New Data arrived");
                        // pDialog.hide();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("manishbutola", "getalltripdetailserror => " + error.toString());
                        new GenerateTokenOperation(ctx).execute();
                      if(new GenerateTokenOperation(ctx).getStatus()==  AsyncTask.Status.FINISHED){
                          getalltripdetails();
                      }
                        /*if(GenerateTokenOperation(ctx).Status == AsyncTask.Status.FINISHED){
                            // START NEW TASK HERE
                        }*/


                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                sharedPreferences = ctx.getSharedPreferences(Constant.MyPREFERENCES, Context.MODE_PRIVATE);
                String token_value = sharedPreferences.getString("token", null);
                String authorizedtoken = "bearer" + " " +token_value;
                //params.put("User-Agent", "Nintendo Gameboy");
                Log.d("packageApiCalls", "authorizedtoken: "+ authorizedtoken);
                params.put("Authorization", authorizedtoken);
                return params;
            }

            @Override
            public Priority getPriority() {
                return priority;
            }
        };
        AppController.getInstance().addToRequestQueue(postRequest);
        //return parsejson;
        // List<AllTrips.TripsBean>  allTrips=db.getAllTripsByMobileUserID(1);
    }
    private List<AllTrips.TripsBean> parsejson(String response) {
        try {
            if(Constant.splashScreenApiCall){
                new ParsingOperation(ctx).execute(response);

            }
            else{
                new ParsingOperation(ctx,alltripsFragment).execute(response);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return allTripsByMobileUserIDNewData1;
    }

}

My `gettodaydate` method as follows:

public String gettodaysdate(){
        DateFormat formatter;
        formatter = new SimpleDateFormat("MM-dd-yyyy",Locale.ENGLISH);
        String result = formatter.format(new Date());

        /*DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        Date date = new Date();*/
//        return dateFormat.format(date);
        return result;
    }

最后是我的 string.xml :

<string name="alltrips_api_ta_url" translatable="false">http://roamer.techmaster.in/Rest/api/GetAgentTrips?agentId="%1$d"&amp;isPreviousTrip=false&amp;date="%2$s"</string>

我正在获取 log cat 作为:

Unexpected response code 400 for http://roamer.techmaster.in/Rest/api/GetTrips?mobileUserId=١&isPreviousTrip=false&date=02-14-2017

我看到 mobileUserId 是 1,当 url 访问服务器时,它将转换为阿拉伯语的 url。应用程序将 crash.Please 帮助我 out.Thanks。

在您的 getHeaders() 中添加 Content-Type

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

同时覆盖 getBodyContentType() 和 return "application/x-www-form-urlencoded; charset=UTF-8";

 @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

经过努力我有了一个想法并且成功了,它是: 我将 int 更改为字符串,例如:
String userId = String.valueOf(mobileUserId); 然后我做 <string name="alltrips_api_ta_url" translatable="false">http://base_url/trips?agentId="%1$d"&amp;isPreviousTrip=false&amp;date="%2$s"</string>

<string name="alltrips_api_ta_url" translatable="false">http://base_url/GetAgentTrips?agentId="%1$s"&amp;isPreviousTrip=false&amp;date="%2$s"</string>

然后我把

String url = ctx.getResources().getString(R.string.alltrips_api_url,userId   ,gettodaysdate);