我正在尝试将我的数据编码为JSON并将其发送到php文件(然后发送到远程数据库)。调用insertmatch时,我希望首先使用contentvalues将值放入SQLite表中,然后将相同的值更改为要发送的JSON。但是我继续遇到未处理的异常错误,当我使用try catch或处理异常时,它会说不使用局部变量响应的值。
关于如何解决这个问题的任何想法?
public long insertMatch(Match match) {
//returns the id of the inserted row; if an error occurs it returns -1
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_TEAM1, match.getTeam1());
values.put(DatabaseHelper.KEY_TEAM2, match.getTeam2());
values.put(DatabaseHelper.KEY_SCORE_TEAM1, match.getScoreTeam1());
values.put(DatabaseHelper.KEY_SCORE_TEAM2, match.getScoreTeam2());
values.put(DatabaseHelper.KEY_START_TIME_STAMP, match.getStartTime());
values.put(DatabaseHelper.KEY_END_TIME_STAMP, match.getEndTime());
values.put(DatabaseHelper.KEY_LOCATION, match.getLocation());
Log.d(TAG,"insertMatch: " + values);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://myhost/data.php");
try {
String team_1 = match.getTeam1();
String team_2 = match.getTeam2();
int score_team_1= match.getScoreTeam1();
int score_team_2 = match.getScoreTeam2();
long start_time = match.getStartTime();
long end_time = match.getEndTime();
Log.d(TAG,"Checking if values ok for jsoning on " + team_1);
Log.d(TAG,"Checking if values ok for jsoning on " + team_2);
Log.d(TAG,"Checking if values ok for jsoning on " + score_team_1);
Log.d(TAG,"Checking if values ok for jsoning on" + score_team_2);
Log.d(TAG,"Checking if values ok for jsoning on " + start_time);
Log.d(TAG,"Checking if values ok for jsoning on " + end_time);
String jString = "{\"Necessary??Perhaps, perhaps not. Only time will tell\": { \"team_1\": \""+team_1 + "\",\"team_2\":\""+team_2+"\",\"score_team_1\":\""+score_team_1+"\",\"score_team_2\":\""+score_team_2+"\",\"start_time\":\""+start_time+"\",\"end_time\":\""+end_time+"\"}}";
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("value", jString));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 not supported");
}
HttpResponse response;//error either here
response = httpclient.execute(httppost); //or here
return db.insert(DatabaseHelper.DATABASE_TABLE, null,values);
}
答案 0 :(得分:0)
您应该复制/粘贴整个错误,因为您没有提供太多信息。
无论如何,通知一个未使用的局部变量是一个奇怪的异常,因为那更可能是一个回归,但无论如何,如果你不使用变量响应,你就不必声明它。所以你的行
HttpResponse response;
response = httpclient.execute(httppost);
应该只是
httpclient.execute(httppost);
无论如何,我认为错误是其他的,可能是你如何构建httpost变量。来自logcat的完整信息将有所帮助。