PDA

View Full Version : post وب سرویس



emperatorsoftware
سه شنبه 27 مرداد 1394, 16:32 عصر
سلام عصر همگی بخیر
یه مشکلی که دارم اینه که داده ها رو برای ثبت در دیتابیس میفرستم ثبت نمیکنه

جالبه که logcat هم هیچی نشون نمیده نه خطا نه اخطار نه اعلان

مچکرم اگه مشکلمو حل کنین

اینم کد برنامه و کد
PHP سرور



public class TestActivity extends Activity {

String result;
private ArrayList<NameValuePair> item;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

final EditText txt_name = (EditText) findViewById(R.id.txt_name);
final EditText txt_family = (EditText) findViewById(R.id.txt_family);
final EditText txt_tel = (EditText) findViewById(R.id.txt_tel);
final EditText txt_address = (EditText) findViewById(R.id.txt_address);
final Button read = (Button) findViewById(R.id.button1);
final Button insert = (Button) findViewById(R.id.button2);
final TextView textView1 = (TextView) findViewById(R.id.textView1);

read.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.1.2/book/?action=read");

try {
HttpResponse response = client.execute(post);
int statuscode = response.getStatusLine().getStatusCode();
Toast.makeText(getBaseContext(), ""+statuscode, Toast.LENGTH_LONG).show();
if(statuscode == 200){
result = StreamToString(response.getEntity().getContent());
if (! result.startsWith("[")){
result = "[" + result;
}
if (! result.endsWith("]")){
result = result + "]";
}
textView1.setText(result);
}
}catch (ClientProtocolException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}

}
});

insert.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
item = new ArrayList<NameValuePair>();
item.add(new BasicNameValuePair("name", txt_name.getText().toString()));
item.add(new BasicNameValuePair("family", txt_family.getText().toString()));
item.add(new BasicNameValuePair("tel", txt_tel.getText().toString()));
item.add(new BasicNameValuePair("address", txt_address.getText().toString()));

HttpPost post = new HttpPost("http://192.168.1.2/book/?action=insert");
Toast.makeText(getBaseContext(), "Post OK", Toast.LENGTH_LONG).show();
try {
post.setEntity(new UrlEncodedFormEntity(item));
Toast.makeText(getBaseContext(), "Send", Toast.LENGTH_LONG).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "unsupport", Toast.LENGTH_LONG).show();
}
/*Runnable runnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

// if (!TextUtils.isEmpty(txtName.getText().toString()))

HttpPost post = new HttpPost("http://192.168.1.2/book/?action=insert");
Toast.makeText(getBaseContext(), "Post OK", Toast.LENGTH_LONG).show();

if (item != null){
try {
post.setEntity(new UrlEncodedFormEntity(item,"UTF-8"));
Toast.makeText(getBaseContext(), "Send", Toast.LENGTH_LONG).show();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "unsupport", Toast.LENGTH_LONG).show();
}
}
if(item == null){
Toast.makeText(getBaseContext(), "nul", Toast.LENGTH_LONG).show();
}

}
};
new Thread(runnable).start();*/

}
});

}

public String getData() {

final StringBuilder sb = new StringBuilder();
try {
JSONArray array = new JSONArray(result);
for (int i = 0 ; i < array.length() ; i++){
JSONObject object = array.getJSONObject(i);
sb.append(object.getLong("id"));
sb.append(object.getString("name"));
sb.append(object.getString("family"));
sb.append(object.getString("tel"));
sb.append(object.getString("address"));

}
return sb.toString();

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

private String StreamToString(InputStream is) {
StringBuilder builder = new StringBuilder();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
int line = -1;
try {
while ((line = bfr.read()) != -1) {
try {
builder.append(bfr.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return builder.toString();
}





<?php


if(isset($_REQUEST['action'])) {
$action = $_REQUEST['action'];
} else {
echo "Error";
exit();
}

if($action == "read") {
read();
} else if($action == "insert") {
insert();
}

function connection() {
$con = mysqli_connect("localhost","root","","book");
if(mysqli_connect_errno()) {
echo "Fail" . mysqli_connect_error();
}
mysqli_set_charset($con,"UTF-8");
//header('Content-type: text/html; charset=UTF-8');
return $con;
}

function read() {
$con = connection();

ini_set('default_charset',"UTF-8");
mysqli_set_charset($con,'utf-8');
header('Content-type: text/html; charset=UTF-8') ;

$result = mysqli_query($con,"SELECT * FROM main");

$output = array();
$fetch_data = array();

while ($row = mysqli_fetch_array($result)) {
$fetch_data['id'] = $row['id'];
$fetch_data['name'] = $row['name'];
$fetch_data['family'] = $row['family'];
$fetch_data['tel'] = $row['tel'];
$fetch_data['address'] = $row['address'];

$output[] = $fetch_data;
}

echo json_encode($output);

mysqli_close($con);
}

function insert() {

$name = $_REQUEST['name'];
$family = $_REQUEST['family'];
$tel = $_REQUEST['tel'];
$address = $_REQUEST['address'];

if( !empty($name) && !empty($family) && !empty($tel) && !empty($address)) {
$con = connection();
mysqli_query('SET NAMES \'utf8\'');
mysqli_set_charset('utf8');
$result = mysqli_query($con,"INSERT INTO 'main' ('id','name','family','tel','address') VALUES ('Null','$name','$family','$tel','$address');");
mysqli_close($con);
}
}


?>

Nevercom
سه شنبه 27 مرداد 1394, 16:40 عصر
تو کد مربوط به دکمه ی insert من نمیبینم که درخواست رو ارسال کرده باشید، فقط دارید درخواست POST رو آماده می کنید اما اجراش نمی کنید (با client.execute)

emperatorsoftware
چهارشنبه 28 مرداد 1394, 15:54 عصر
تو کد مربوط به دکمه ی insert من نمیبینم که درخواست رو ارسال کرده باشید، فقط دارید درخواست POST رو آماده می کنید اما اجراش نمی کنید (با client.execute)

سلام
بجای کدهای خط 74 تا 101 که صرفا کامنت بودن اینا رو نوشتم بازم نشد

نکنه کد PHP من مشکل داره؟؟؟؟



try {
response = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Nevercom
چهارشنبه 28 مرداد 1394, 16:06 عصر
کوئری تون هم ایراداتی داره، باید فیلد ها ( نام جدول و فیلدها) رو بین ` قرار بدید و مقادیر رو بین '

emperatorsoftware
چهارشنبه 28 مرداد 1394, 22:32 عصر
کوئری تون هم ایراداتی داره، باید فیلد ها ( نام جدول و فیلدها) رو بین ` قرار بدید و مقادیر رو بین '


خیلی ممنون از لطفتون مشکلم حل شد و ثبت میشه در بانک

بابت پشتیبانی و راهنمایی های دقیق مچکرم:لبخندساده: