我很新的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();
会有什么问题?
什么是GET_DATA? –
其我的Url地址http://example.com/user/auth – Pranita