2017-06-29 106 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