我正在開發一個Android項目,在移動本地語言爲阿拉伯語時,我必須撥打APIs
英語。我沒有得到API
URl
String
英文。我的API調用文件(PackageapiCall.class
)如下:在Android手機上使用本地語言爲阿拉伯語時調用API
package 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"&isPreviousTrip=false&date="%2$s"</string>
我媽獲取日誌貓爲:
Unexpected response code 400 for http://roamer.techmaster.in/Rest/api/GetTrips?mobileUserId=١&isPreviousTrip=false&date=02-14-2017
,因爲我看到的是mobileUserId爲1,它是要在URL轉換成阿拉伯語時,網址將命中服務器。應用程序將崩潰。請幫我。謝謝。
哪裏是U變更語言? – rafsanahmad007
當地語言或其他什麼? –
其中是更改語言環境的代碼?並在你的'StringRequest'中如何添加參數? – rafsanahmad007