如何从.net中的web服务获取marathi数据到spinner?

时间:2018-03-23 07:44:46

标签: android json json.net

我正在尝试使用昨天答案的另一个文本

Just taken one text view and in on create only tried to apply custom font

输出仍未获得Output is like this

当前输出

enter image description here

见下面的代码

enter image description here

它在模拟器中显示如下

{"Get_DepoScheme": [{
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 1,
            "SchemeName": "मुदत ठेव",
            "Glid": 55
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 2,
            "SchemeName": "कर्मचारी भ.नि.नि.मुदत ठेव",
            "Glid": 38
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 3,
            "SchemeName": "सभासद कायम ठेव",
            "Glid": 14
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 5,
            "SchemeName": "आवर्ती ठेव (आर.डी.)",
            "Glid": 51
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 6,
            "SchemeName": "सुकन्या ठेव योजना",
            "Glid": 77
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 7,
            "SchemeName": "स्वामी पेन्शन ठेव योजना",
            "Glid": 78
        }, {
            "Flag": 1,
            "Msg": "Success",
            "SchemeId": 16,
            "SchemeName": "दामदुप्पट ठेव",
            "Glid": 54
        }]
    }

2 个答案:

答案 0 :(得分:0)

尝试URLDecoder,例如试试这个

String flag = URLDecoder.decode(json_data.getString("Flag"), "UTF-8");

并将文本设置为textview,如下所示

     TextView tv = (TextView) findViewById(R.id.tv);
     Typeface marathi = Typeface.createFromAsset(getAssets(), "akshar.ttf");
     tv.setTypeface(marathi);
     tv.setText(flag);

here下载 marathi 字体并在项目中创建资产文件夹并将其添加到那里。(资产位置app>src>main>assets>akshar.ttf

输出 - >>

enter image description here

答案 1 :(得分:0)

使用了AsyncTask:

public class TestMarathiData extends ActionBarActivity
{
    InputStream is = null;
    String result = null,Uname;
    String line = null;
    ArrayAdapter<String> adapter;
    ArrayList list1;
    boolean isInternetConn;
    Button btn1;
    TelephonyManager tel;
    String IMEI;
    ProgressDialog progressDialog ;
    String GetSchemePath="URL here";

   HttpURLConnection httpURLConnection ;
    URL url;
    OutputStream outputStream;
    BufferedWriter bufferedWriter ;
    int RC ;
    BufferedReader bufferedReader ;
    StringBuilder stringBuilder;
    boolean check = true;
    TextView txtv_scheme;
    Spinner SpinnerScheme;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_marathi_data);
        ///// HTTP code ////
        if (Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //
        /////// to get IMEI number ///////
        tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = tel.getDeviceId().toString();
        //
        //Global Variables
        final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
        //

       Uname = globalVariable.getUID();

        list1=new ArrayList<String>();
        txtv_scheme = (TextView)findViewById(R.id.txtv_scheme);
        btn1 = (Button)findViewById(R.id.btn1);
        SpinnerScheme = (Spinner)findViewById(R.id.SpinnerScheme);

        btn1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                GetDepositScheme();
            }
        });
    }//EOF OnCreate
    //GetDepositScheme
    private void GetDepositScheme()
    {
        class AsyncTaskGetScheme extends AsyncTask<Void,Void,String>
        {
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
                progressDialog = ProgressDialog.show(TestMarathiData.this,"Fetching Data","Please Wait",false,false);
            }
            @Override
            protected void onPostExecute(String string1)
            {
                super.onPostExecute(string1);
                try
                {
                    JSONObject json = new JSONObject(result);
                    JSONArray jArray = json.getJSONArray("Get_DepoScheme");
                    List<String> l = new ArrayList<String>();
                    for (int i = 0; i < jArray.length(); i++)
                    {
                        JSONObject json_data = jArray.getJSONObject(i);
                        String SchemeId = json_data.getString("SchemeId");
                        String SchemeName = json_data.getString("SchemeName");
                        //
                        l.add(SchemeId+"-"+SchemeName);
                        //
                        adapter = new ArrayAdapter<String>(TestMarathiData.this,R.layout.spinner_item,l)
                        {
                                public View getView(int position, View convertView, ViewGroup parent)
                                {
                                    View v = super.getView(position, convertView, parent);
                                    Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/ARIALUNI.TTF");
                                    ((TextView) v).setTypeface(externalFont);
                                    return v;
                                }
                            public View getDropDownView(int position, View convertView, ViewGroup parent)
                            {
                                    View v = super.getDropDownView(position, convertView, parent);
                                    Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/akshar.ttf");
                                    ((TextView) v).setTypeface(externalFont);
                                    v.setBackgroundColor(Color.WHITE);
                                    return v;
                                }
                            };
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        SpinnerScheme.setAdapter(adapter);
                    }  // for
                    progressDialog.dismiss();
                    SpinnerScheme.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
                    {
                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
                        {
                            String Spinner_item = SpinnerScheme.getSelectedItem().toString();
                            Toast.makeText(getApplicationContext(), "Selected :: " + Spinner_item, Toast.LENGTH_SHORT).show();
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> arg0)
                        {}
                    });//
                }
                catch(Exception e)
                {
                    Log.e("Fail 3", e.toString());
                    Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            protected String doInBackground(Void... params)
            {
                GetSchemeClass getschemeClass = new GetSchemeClass();
                HashMap<String,String> HashMapParams = new HashMap<String,String>();
                HashMapParams.put("Username",Uname);

               String FinalData = getschemeClass.GetSchemeHttpRequest(GetSchemePath,HashMapParams);
                return FinalData;
            }
        }
        AsyncTaskGetScheme AsyncTaskGetSchemeOBJ = new AsyncTaskGetScheme();
        AsyncTaskGetSchemeOBJ.execute();
    }//EOF GetDepositScheme
    //GetSchemeClass
    private class GetSchemeClass
    {
        public String GetSchemeHttpRequest(String requestURL, HashMap<String, String> PData)
        {
            StringBuilder stringBuilder = new StringBuilder();
            try
            {
                url = new URL(requestURL);
                httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setReadTimeout(20000);
                httpURLConnection.setConnectTimeout(20000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                outputStream = httpURLConnection.getOutputStream();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                bufferedWriter.write(bufferedWriterDataFN(PData));
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                RC = httpURLConnection.getResponseCode();
                if (RC == HttpsURLConnection.HTTP_OK)
                {
                    bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                    stringBuilder = new StringBuilder();
                    String RC2;
                    while ((RC2 = bufferedReader.readLine()) != null)
                    {
                        stringBuilder.append(RC2);
                    }
                }
                result = stringBuilder.toString();
                Log.d("Result", "Result" + result);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }
        private String bufferedWriterDataFN(HashMap<String, String> HashMapParams) throws UnsupportedEncodingException
        {
            stringBuilder = new StringBuilder();
            for (Map.Entry<String, String> KEY : HashMapParams.entrySet())
            {
                if (check)
                    check = false;
                else
                    stringBuilder.append("&");
                stringBuilder.append(URLEncoder.encode(KEY.getKey(), "UTF-8"));
                stringBuilder.append("=");
                stringBuilder.append(URLEncoder.encode(KEY.getValue(), "UTF-8"));
            }
            return stringBuilder.toString();
        }
    }//EOF GetSchemeClass
}//EOF Main