这是我的第二项活动。当用户单击该按钮时,语言将从英语更改为阿拉伯语。我还使用Internet来检索一些数据。问题是我不知道如何从互联网上检索数据。我该怎么做?请指导。
package com.moqawalat.lang;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Locale;
import static com.moqawalat.lang.R.id.tv;
import static com.moqawalat.lang.R.id.tv1;
public class Second extends AppCompatActivity {
Button okbtn;
TextView countrytxt, languagetxt;
SharedPreferences pref;
String getcountry, getlanguage, a, b, c;
TextView tv, tv1;
JSONParser jsonparser = new JSONParser();
JSONObject jsonobject = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
pref = getSharedPreferences("CountryPref", MODE_PRIVATE);
countrytxt = (TextView) findViewById(R.id.country);
languagetxt = (TextView) findViewById(R.id.language);
tv = (TextView) findViewById(R.id.tv);
tv1 = (TextView) findViewById(R.id.tv1);
okbtn = (Button) findViewById(R.id.okbtn);
getcountry = pref.getString("country", "");
getlanguage = pref.getString("language", "");
okbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (getlanguage.equalsIgnoreCase("en")) {
Locale locale = new Locale(getlanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences sp = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("language", getlanguage);
edit.commit();
countrytxt.setText("Country :" + getlanguage);
fragone();
Intent i = new Intent(Second.this, Second.class);
startActivity(i);
} else if (getlanguage.equalsIgnoreCase("ar")) {
Locale locale = new Locale(getlanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences sp = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("language", getlanguage);
edit.commit();
countrytxt.setText("Country :" + getlanguage);
Intent i = getIntent();
finish();
startActivity(i);
}
}
});
}
private void fragone() {
this.setContentView(R.layout.activity_second);
new retrievedata().execute();
return;
}
class retrievedata extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(Second.this,"Json Data is downloading",Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... strings) {
jsonobject = jsonparser.makeHttpRequest("http://www.json-generator.com/api/json/get/cegNjwgBTS?indent=2");
Log.d("app data", jsonobject.toString());
try {
a = jsonobject.getString("name");
b = jsonobject.getString("data");
c = jsonobject.getString("image_url");
} catch (JSONException e){
e.printStackTrace();
}
return a;
}
protected void onPostExecute(String a){
tv.setText(a);
tv1.setText(b);
ImageView image = (ImageView) findViewById(R.id.img);
Picasso.with(Second.this).load(c).error(R.drawable.error).placeholder(R.drawable.progress_aniamtion).noFade().into(image);
}
}
}