Я использую AsyncTask и метод GET для отправки переменной на сервер, получения объекта JSON и анализа объекта JSON, все работает!
Но теперь я хочу изменить свой метод, использующий метод POST для отправки переменной на сервер, возврата объекта JSON и анализа объекта JSON, а не использования метода GET.
Что я могуdo?
Вот мой код, который использует метод GET для отправки переменной на сервер, получения объекта JSON и анализа объекта JSON:
public class MainActivity extends AppCompatActivity {
public static String TAG = "MainActivity";
public static String deviceIMEI;
public static String token;
TelephonyManager telephonyManager;
public static String memoryToken;
public static SharedPreferences setting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
memoryToken = null;
deviceId();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
deviceIMEI = telephonyManager.getDeviceId();
if(deviceIMEI != null){
setting = getSharedPreferences("UserData", MODE_PRIVATE);
memoryToken = setting.getString("Token", "");
if(memoryToken != null && memoryToken != ""){
Intent i = new Intent(MainActivity.this, CoverActivity.class);
startActivity(i);
}
else if(memoryToken == ""){
new JSONTask().execute("api1" + "?deviceId=" + deviceIMEI + "&os=0");
Intent i = new Intent(MainActivity.this, CoverActivity.class);
startActivity(i);
}
else{
}
}
else{
}
}
private void deviceId() {
telephonyManager = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
return;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
return;
}
String iemi = telephonyManager.getDeviceId();
deviceIMEI = iemi;
if(deviceIMEI != null){
new JSONTask().execute("api1" + "?deviceId=" + deviceIMEI + "&os=0");
Intent i = new Intent(MainActivity.this, CoverActivity.class);
startActivity(i);
}
} else {
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public class JSONTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
try{
JSONObject jsonObject = new JSONObject(finalJson);
String code = jsonObject.getString("code");
String desc = jsonObject.getString("desc");
if(Integer.valueOf(code) == 1){
JSONObject userInfo = jsonObject.getJSONObject("userInfo");
token = userInfo.getString("token");
Log.e(TAG, "token in JSONTask : " + token);
setting = getSharedPreferences("UserData", MODE_PRIVATE);
setting.edit().clear().commit();
setting.edit().putString("Token", token).commit();
memoryToken = setting.getString("Token", "");
}
else{
}
} catch (JSONException e) {
e.printStackTrace();
}
//return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
}