public class Register_ extends Activity{
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText name;
EditText password;
Button login_button;
private static String url = "http://192.168.1.203/EnjoyDemo/test.php";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
name=(EditText)findViewById(R.id.editText1);
password=(EditText)findViewById(R.id.editText2);
login_button=(Button)findViewById(R.id.button1);
login_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(validate()){
new Up().execute();
}
}
});
}
private boolean validate()
{
String name1 = name.getText().toString().trim();
if (name1.equals(""))
{
DialogUtil.showDialog(this, "����û����д����", false);
return false;
}
String password1 = password.getText().toString().trim();
if (password1.equals(""))
{
DialogUtil.showDialog(this, "����û����д����", false);
return false;
}
return true;
}
class Up extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register_.this);
pDialog.setMessage("�����ϴ�..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String name1 = name.getText().toString();
String password1 = password.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name1));
params.add(new BasicNameValuePair("password", password1));
try{
JSONObject json = jsonParser.makeHttpRequest(url,
"POST", params);
String message = json.getString(TAG_MESSAGE);
return message;
}catch(Exception e){
e.printStackTrace();
return "";
}
}
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
Log.d("json", json.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
做注册界面传输json字符串时出现了这个错误 菜鸟初学 没有指导不知道该怎么做了。。。有没有人指导下怎么改 谢谢了 只有这个错误显示
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject