2014-09-28 2 views
0

브로드 캐스트 수신기가 "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()); 
     } 
.... 

아무도 아이디어가 있습니까?

답변

0

당신이 당신의 텍스트 뷰에 표시된 것과 같은 유효한 경로를 사용하려는 경우 경로가 올바르게 표시 되었기 때문에, 다음 tv4.getText().toString()를 사용하여 makingJson() 기능

+0

제안 해 주셔서 감사합니다. TextView를 전역 변수로 정의 할 수는 없지만 해결 방법을 찾았습니다. – Mikosch

0

에 값을 전달하는 전역 변수로 TextView tv4를 넣어 시도 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); 
     .... 

아마도 가장 우아한 방법은 아니지만 효과가 있을지 모릅니다.