2014-09-28 50 views
0

我的廣播接收器通過「newIntent.putExtra」在變量中提交路徑。該路徑在接收器在TextView中調用的活動中正確顯示,但是當我使用該變量創建JSONObject時,將使用它初始化的值代替。我嘗試上傳的服務器請求JSON字符串進行授權。JSON對象內容「舊」值

@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()); 
     } 
.... 

有沒有人有想法?

回答

0

如果你想使用相同的有效路徑顯示在您的TextView,儘量把你的TextView tv4爲全局變量,然後使用tv4.getText().toString()的值傳遞給你的makingJson()功能

+0

謝謝您的建議!我無法將TextView定義爲全局變量,但我找到了一個解決方法: – Mikosch 2014-09-29 21:01:17

0

由於路徑是正確顯示TextView的我終於把它的內容到另一個變量:

public JSONObject makingJson() { 
     final TextView textViewPath = (TextView) findViewById(R.id.viewPath); 
     String path = textViewPath.getText().toString 
     JSONObject jsonObject = new JSONObject(); 
     try { 
      jsonObject.put("username", API_USER); 
      jsonObject.put("password", API_PASS); 
      jsonObject.put("name", name); 
      jsonObject.put("file_location", path); 
     .... 

也許這不是最優雅的方式,但它的工作。