2015-04-23 50 views
0

我想通過HttpURLConnection API將數據發佈到本地主機服務器(WAMP)。我在IntentService的幫助下實現了它,但我不知道如何將數據「JSON字符串」從MainActivity傳遞到DataPost類。Android:將JSON字符串傳遞給服務類「PostData」

這部分正在從onLocationChanged梅索德稱爲內部類 「MyLocationListener」 在MainActivity:

 String jSONString = convertToJSON(pLong, pLat, formatted); 

     Intent intent3 = new Intent(MainActivity.this, PostData.class); 
     intent3.putExtra("json_data", jSONString); 
     PendingIntent pintent3 = PendingIntent.getService(getApplicationContext(), 0, intent3, 0); 
     AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     Calendar cal = Calendar.getInstance(); 
     // for 30 mint 60*60*1000 
     alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
       1000, pintent3); 

     startService(intent3); 

POSTDATA類:

package com.bustracker; 

import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.IntentService; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Handler; 

public class PostData extends IntentService { 
    String jSONString; 
    Handler handler = new Handler(); 

    public PostData() { 
     super("some"); 

    } 

    public String getjSONString() { 
     return jSONString; 
    } 

    public void setjSONString(String jSONString) { 
     this.jSONString = jSONString; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // TODO Auto-generated method stub 
     String jSONString = intent.getStringExtra("json_data"); 
     try { 
      // This is the ip address of my laptop wifi because I am running the 
      // app in my device and I want to send the data to the localhost 
      // server(WAMP). 
      URL myUrl = new URL("http://192.168.x.x/webservice"); 
      HttpURLConnection myConnection = (HttpURLConnection) myUrl 
        .openConnection(); 
      myConnection.setRequestMethod("POST"); 
      myConnection.setDoOutput(true); 
      myConnection.setUseCaches(false); 
      myConnection.setConnectTimeout(10000); 
      myConnection.setReadTimeout(10000); 
      myConnection.setRequestProperty("Content-Type", "application/json"); 
      myConnection.connect(); 
      // create data output stream 
      DataOutputStream wr = new DataOutputStream(
        myConnection.getOutputStream()); 
      // write to the output stream from the string 
      wr.writeBytes(jSONString); 
      wr.close(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

    } 

} 
+0

爲什麼Intent服務中存在Asynctask? – Raghunandan

+0

好的,我會在稍後得到它:)我認爲它會以這種方式工作。 –

回答

1

我不知道我該怎麼過從MainActivity 到DataPost類的數據「JSON字符串」

使用Intent.putExtra通過jSONString字符串IntentService

.... 
// add jSONString to intent using any key 
intent3.putExtra("json_data",jSONString); 
startService(intent3); 

現在使用intent.getStringExtraonHandleIntent得到字符串:

@Override 
    protected void onHandleIntent(Intent intent) { 
     // TODO Auto-generated method stub 
    String jSONString = intent.getStringExtra("json_data"); 
    } 

注:沒有必要IntentService因爲onHandleIntent方法是使用AsyncTask在工作線程上調用,請求處理

所以使用onHandleIntent方法制作的HTTP請求

+0

這是「意圖」這裏比「intent3」是另一個嗎? –

+0

@MrAsker:沒有看到我的編輯回答 –

+0

@MrAsker:爲什麼投了票?\ –

0

您可以使用putExtra這樣,

Intent i = new Intent(getApplicationContext(), PostData.class); 
x="3"; 
y="A"; 
i.putExtra(ValueX, x); 
i.putExtra(ValueY, y); 
startActivity(i); 

看到客戶將如何接收並嘗試分析,這應該不難。

相關問題