2017-02-14 57 views
0

我正在开发一个Android项目,在移动本地语言为阿拉伯语时,我必须拨打APIs英语。我没有得到APIURlString英文。我的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"&amp;isPreviousTrip=false&amp;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转换成阿拉伯语时,网址将命中服务器。应用程序将崩溃。请帮我。谢谢。

+0

哪里是U变更语言? – rafsanahmad007

+0

当地语言或其他什么? –

+0

其中是更改语言环境的代码?并在你的'StringRequest'中如何添加参数? – rafsanahmad007

回答

0

斗争我有一个想法,它的工作后,它是: 我INT更改为字符串如:
String userId = String.valueOf(mobileUserId);然后我做 < 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>

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

,然后我把

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

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()并返回"application/x-www-form-urlencoded; charset=UTF-8";

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

我已经做到了,但我得到相同的问题和日志,如下所示:BasicNetwork.performRequest:对于http://roamer.techmaster.in/Rest/api/GetTrips?mobileUserId=1&isPreviousTrip=false&date=02-14意外的响应代码400 -2017 –

+0

当语言环境是英语你的api给予回应? – rafsanahmad007

+0

是的,它工作正常当语言环境是英语,但我的问题是当语言环境是阿拉伯语而不是英语。 –