브로드 캐스트 수신기가 "newIntent.putExtra"를 통해 변수에 경로를 제출합니다. 경로는 TextView의 수신기에서 호출 한 액티비티에서 올바르게 표시되지만, JSONObject를 생성하기 위해 변수를 사용할 때 초기화 된 값이 대신 사용됩니다. 업로드를 시도하는 서버가 승인을 위해 JSON-String을 요청합니다.JSON 객체 내용 "old"값
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
new Loader().execute();
TextView tv4 = (TextView) findViewById(R.id.viewPath)
tv4.setText(getIntent().getExtras().getString(BroadcastReceiver.EXTRA_PATH));
}
class Loader extends AsyncTask<Void, Void, JSONObject>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
//different SDK-Versions
if (android.os.Build.VERSION.SDK_INT >= 11) {
dialog = new ProgressDialog(UploadToServer.this, ProgressDialog.THEME_HOLO_LIGHT);
}
else {
dialog = new ProgressDialog(UploadToServer.this);
}
dialog.setMessage(Html.fromHtml("<b>"+"Loading..."+"</b>"));
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
@Override
protected JSONObject doInBackground(Void... params) {
return postJsonObject("Put your url which takes json object", makingJson());
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
if (result!=null) {
dialog.dismiss();
}
else {
Toast.makeText(UploadToServer.this, "Successfully posted json object", Toast.LENGTH_LONG).show();
}
}
}
public JSONObject makingJson() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", API_USER);
jsonObject.put("password", API_PASS);
jsonObject.put("name", name);
jsonObject.put("apk_file_location", BroadcastReceiver.EXTRA_PATH);
}
catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
public JSONObject postJsonObject(String urlServer, JSONObject jsonObject){
InputStream inputStream = null;
String result = "";
try {
DefaultHttpClient httpclient = new DefaultHttpClient(); // Create HttpClient
URI url = new URI(URLEncoder.encode(urlServer, "UTF-8"));
HttpPost httpPost = new HttpPost(url);
String json = "";
json = jsonObject.toString();
System.out.println(json);
Toast.makeText(UploadToServer.this, "json object: "+json, Toast.LENGTH_LONG).show();
StringEntity se = new StringEntity(json)
httpPost.setEntity(se); // Set httpPost Entity
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "postJsonObject did not work!";
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
....
아무도 아이디어가 있습니까?
제안 해 주셔서 감사합니다. TextView를 전역 변수로 정의 할 수는 없지만 해결 방법을 찾았습니다. – Mikosch