2017-06-29 110 views
-3

我很新的JSON使用Java API處理文件未發現異常,同時發佈的數據到服務器的android

我想通過ID和密碼的服務器和想要檢索與該ID.I數據m使用Java API文件發送數據而不是PHP文件。

這裏是我的代碼:

來讀取資產

public String loadJSONFromAsset() { 
    String json = null; 
    try { 
     InputStream is = getActivity().getAssets().open("auth.json"); 
     int size = is.available(); 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 
     json = new String(buffer, "UTF-8"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
     return null; 
    } 
    return json; 
} 

功能到參數值附加到JSON文件

public void save() { 
    try { 
     JSONObject obj = new JSONObject(loadJSONFromAsset()); 
     //JSONArray arr = obj.getJSONArray(""); 
     username = etUsername.getText().toString().trim(); 
     password = etPassword.getText().toString().trim(); 
     obj.put("sso_id", username); 
     obj.put("password", password); 

     jsonArray.put(obj); 
     Log.v("AUTHENTICATE", "Data is " + jsonArray.toString()); 
     JSONObject response; 
     try { 
      Log.v("AUTHENTICATE", jsonArray.toString()); 
      //response = helper.sendJsonData(jsonArray.toString(),GET_DATA) ; 
      new SendDataToServer().execute(String.valueOf(obj)); 
      //new GetData().execute(); 
      //Log.v("AUTHENTICATE", "Response is " + response.toString()); 
     } catch (Exception e) { 
      Log.v("AUTHENTICATE", "Exception in response is " + e.toString()); 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


} 

的AsyncTask代碼以.json文件致電Url

String JsonResponse = null; 
     String JsonDATA = params[0]; 
     HttpURLConnection urlConnection = null; 
     BufferedReader reader = null; 
     try { 
      URL url = new URL(GET_DATA); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setDoOutput(true); 
      // is output buffer writter 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setRequestProperty("Content-Type", "application/json"); 
      urlConnection.setRequestProperty("Accept", "application/json"); 
      //urlConnection.setRequestProperty("Accept", "*/*"); 
      //set headers and method 
      Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8")); 
      writer.write(JsonDATA); 
      // json data 
      writer.close(); 
      InputStream inputStream = urlConnection.getInputStream(); 
      //input stream 
      StringBuffer buffer = new StringBuffer(); 
      if (inputStream == null) { 
       // Nothing to do. 
       return null; 
      } 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      String inputLine; 
      while ((inputLine = reader.readLine()) != null) 
       buffer.append(inputLine + "\n"); 
      if (buffer.length() == 0) { 
       // Stream was empty. No point in parsing. 
       return null; 
      } 
      JsonResponse = buffer.toString(); 
      //response data 
      Log.i("AUTHENTICATE", JsonResponse); 
      //send to post execute 
      return JsonResponse; 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (urlConnection != null) { 
       urlConnection.disconnect(); 
      } 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (final IOException e) { 
        Log.e("AUTHENTICATE", "Error closing stream", e); 
       } 
      } 
     } 
     return null; 

} 

以.json文件

{"sso_id":"", 
"password":""} 

讓我異常

java.io.FileNotFoundException: http://example.com/user/auth 

下面Line在異步任務

InputStream inputStream = urlConnection.getInputStream(); 

會有什麼問題?

+0

什麼是GET_DATA? –

+0

其我的Url地址http://example.com/user/auth – Pranita

回答

0

使用java.net.URL#openStream()使用正確的URL(包括協議!)。例如。

InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream(); 

如何在寫入和讀取數據時使用相同的請求對象POST?它不能用於讀取數據!將其更改爲GET。

此外,您需要更改到urlConnection.setDoOutput(false);

+0

不工作..給我UnknownHostException.also嘗試使用GET方法和urlConnection.setDoOutput(false);但不是在這兩種情況下工作 – Pranita

+0

我希望你使用GET_DATA而不是「http://www.somewebsite.com/a.txt」:D –

+0

GET_DATA是常量字符串值,它是myDomainName(Here example.com)「示例.com/user/auth「 – Pranita