Java JSON对象返回空指针
我想在数据库中插入一些项目。在主活动中,我从用户检索信息并将其传递给另一个类以进行一些解析。我的JSONObject一直显示为NULL 如果我不清楚这个问题,我很抱歉。我已经尽可能多地解释了 下面是欢迎您输入的代码Java JSON对象返回空指针,java,android,json,android-manifest,Java,Android,Json,Android Manifest,我想在数据库中插入一些项目。在主活动中,我从用户检索信息并将其传递给另一个类以进行一些解析。我的JSONObject一直显示为NULL 如果我不清楚这个问题,我很抱歉。我已经尽可能多地解释了 下面是欢迎您输入的代码 public class MainActivity extends Activity { /** THE FOLLOWING STRINGS WILL BE DISPLAYED IN LOGCAT */ final String TAG = "########-------MAIN
public class MainActivity extends Activity {
/** THE FOLLOWING STRINGS WILL BE DISPLAYED IN LOGCAT */
final String TAG = "########-------MAIN ACTIVITY: LOGIN--------######";
final String URL = "http://46.51.193.32/timereport/ses/sessions";
UserHelper userAdapter;
UserHelper db;
EditText edit_password,edit_username,edit_company;
String regName;
int duration = Toast.LENGTH_LONG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new UserHelper(this);
userAdapter = new UserHelper(this);
edit_password = (EditText)findViewById(R.id.password);
edit_username = (EditText)findViewById(R.id.user_name);
edit_company = (EditText)findViewById(R.id.company_string);
Button login = (Button)findViewById(R.id.login_button);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
JSONObject jsonobj = new JSONObject();
try{
JSONObject subJson = new JSONObject();
subJson.put("username", edit_username.getText().toString());
subJson.put("password", edit_password.getText().toString());
subJson.put("company", edit_company.getText().toString());
jsonobj.put("user", subJson);
}
catch(JSONException e) {
Log.i("","#####-----error at catch jsonexception-----#####");
}
HandleJSON.SendHttpPost(URL, jsonobj);
String regNameSplit[] = regName.split("-");
try{
userAdapter.openDatabase();
long id = db.insertIntoDatabase(edit_username.getText().toString(),edit_company.getText().toString(), edit_password.getText().toString(),regNameSplit[0], regNameSplit[2]);
Toast.makeText(getApplicationContext(), "You have successfully logged in as: " +"\n" +regNameSplit[0], duration).show();
Log.i(TAG, "Printing value of id which will be inserted only to remove warnings "+id);
userAdapter.closeDatabase();
}
catch(SQLiteException e){
e.printStackTrace();
}
}
});
}
}
这是我将要解析的JSON对象发送到的类
public class HandleJSON{
UserHelper userAdapter;
private static final String TAG = "&&----HTTPClient-----**";
public static String SendHttpPost (String URL, JSONObject jsonobj) {
String regName = "";
try{
Log.v("Json object request is ",jsonobj.toString());
DefaultHttpClient httpClientInstance = GetHttpClient.getHttpClientInstance();
HttpPost httpPostRequest = new HttpPost(URL);
Log.v(TAG,"The url is "+URL);
StringEntity se;
se = new StringEntity(jsonobj.toString());
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpClientInstance.execute(httpPostRequest);
Log.i(TAG, "HTTPRESPONSE RECIEVED" +(System.currentTimeMillis()-t) + "ms");
String resultString = convertStreamToString(response.getEntity().getContent());
Log.v(TAG , "The response is " +resultString);
JSONObject jsonObj = new JSONObject(resultString);
JSONObject sessionJson = jsonObj.getJSONObject("session");
String sessionId = sessionJson.getString("sessionid");
String name = sessionJson.getString("name");
Log.v(TAG,"The session ID is "+sessionId);
Log.v(TAG,"The name is "+name);
regName = name+"-"+sessionId+"-"+URL;
} catch (Exception e){
e.printStackTrace();
}
return regName;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try{
while((line = reader.readLine()) !=null ){
sb.append(line + "\n");
}
}
catch (IOException e){
e.printStackTrace();
} finally{
try {
is.close();
} catch (IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
我刚刚添加了main活动中缺少的一些代码
String regNameSplit[] = regName.split("-");
一直显示为空
convertStreamToString()
方法异常
,这将隐藏未检查的(运行时)异常。您可能正在隐藏中发生的JSONException
SendHttpPost
,但未将结果分配给变量:
HandleJSON.SendHttpPost(URL, jsonobj);
应该是:
regName = HandleJSON.SendHttpPost(URL, jsonobj);
convertStreamToString()
方法异常
,这将隐藏未检查的(运行时)异常。您可能正在隐藏中发生的JSONException
SendHttpPost
,但未将结果分配给变量:
HandleJSON.SendHttpPost(URL, jsonobj);
应该是:
regName = HandleJSON.SendHttpPost(URL, jsonobj);
我看不出有什么问题,你能告诉我regname有什么用吗 在主要活动中,只需更改以下内容:
regname = HandleJSON.SendHttpPost(URL, jsonobj);
您没有回拨要分配给sendhttppost返回的name和sessionid的regname。我看不出有什么问题,您能告诉我regname的用途吗 在主要活动中,只需更改以下内容:
regname = HandleJSON.SendHttpPost(URL, jsonobj);
您没有回调要分配给您在sendhttppost返回的name和sessionid的regname。请尝试在HandleJSON.sendhttppost(URL,jsonobj)之前打印json字符串,我认为您可能遇到了异常,在这种情况下jsonobj将为null。通过“保持显示为null”,您的意思是
SendHttpPost
中的jsonobj参数为null吗?顺便说一句,Log.v(“Json对象请求为”,jsonobj.toString())
看起来有点奇怪,你是说Log.v(“Json对象请求是”+jsonobj.toString())代码>?他可能是指Log.v(标记,“Json对象请求是”+jsonobj)
您没有使用SendHttpPost()
方法的结果。请参阅下面我的更新答案。请尝试在HandleJSON.SendHttpPost(URL,jsonobj)之前打印json字符串,我认为您可能遇到了异常,在这种情况下jsonobj将为null。通过“保持显示为null”,您的意思是SendHttpPost
中的jsonobj参数为null吗?顺便说一句,Log.v(“Json对象请求为”,jsonobj.toString())
看起来有点奇怪,你是说Log.v(“Json对象请求是”+jsonobj.toString())代码>?他可能是指Log.v(标记,“Json对象请求是”+jsonobj)
您没有使用SendHttpPost()
方法的结果。请参阅下面我的更新答案。。。。在Android的Log
类中,当日志记录非常方便时,千万不要使用printStackTrace()
printStackTrace()
切断堆栈跟踪,并具有精确处理感兴趣部分的天赋。。。。在Android的Log
类中,当日志记录非常方便时,千万不要使用printStackTrace()
printStackTrace()
可以切断堆栈跟踪,并且能够精确地处理感兴趣的部分。