2016-12-28 35 views
0

我是新來的android開發!如何在json對象中傳遞字符串?

我試圖通過字符串中JSON對象

代碼:

String URL="http://some ip address"; //Here i have to call clientInfo 

protected JSONObject doInBackground(String... args) { 


     String no_of_fishes = args[3]; 
     String no_of_alarms = args[2]; 
     String password = args[1]; 
     String username= args[0]; 

     ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("username", username)); 
     params.add(new BasicNameValuePair("password", password)); 
     params.add(new BasicNameValuePair("no_of_alarms", no_of_alarms)); 
     params.add(new BasicNameValuePair("no_of_fishes", no_of_fishes)); 

     JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params); 

     return json; 

    } 


public static ArrayList<String> getClientList() { 
    ArrayList<String> clientList = new ArrayList<>(); 
    BufferedReader br = null; 
    try { 
     br = new BufferedReader(new FileReader("/proc/net/arp")); 
     String line; 
     while ((line = br.readLine()) != null) { 
      String[] clientInfo = line.split(" +"); 
      String mac = clientInfo[3]; 
      if (mac.matches("..:..:..:..:..:..")) { 
       clientList.add(clientInfo[0]); 
       ipadd.setText("Client Ip:" + clientInfo[0]); 
      } 
     } 
    } catch (java.io.IOException aE) { 
     aE.printStackTrace(); 
     return null; 
    } 
    return clientList; 
} 

下如何調用clinentInfo在字符串URL,我是新來的Java和Android請任意一個幫助如何做這個。

+0

你可以對你想要什麼更具體的是什麼實現 –

+0

可能的重複[如何將字符串轉換爲Java中的JSONObject](http://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java) –

+0

params值應該是發送到客戶端ip –

回答

0

您可以使用下面的代碼

JSONObject jsonobj= new JSONObject();//create json object 
jsonobj.put("name", new JSONArray(clientInfo[])); 
0

如果我理解正確的把ClientInfo客戶端陣列JSON,這是你想要

JSONArray clients= new JSONArray(getClientList()); 
JSONObject res = new JSONObject(); 
res.put("clientlist",clients); 
相關問題