使用Slim框架

时间:2017-01-09 05:27:36

标签: php android web-services retrofit slim

我正在使用slim框架实现Retrofit,但它无法正常工作。

当我使用chrome中的邮递员测试我的Web api(PHP超薄代码)时,它工作正常,并且在Android端数据传递URL,我也可以在logcat中看到。 这是我的代码。

    <?php
$app->post('/directorder',function($request) {

    require_once('dbconnect.php');

    $query = "INSERT INTO  direct_order (d_name,d_contact,d_address) values (?,?,?)";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("sss",$a,$b,$c);


        $a = $request->getParsedBody()['d_name'];
        $b = $request->getParsedBody()['d_contact'];
        $c = $request->getParsedBody()['d_address'];

    if ($stmt->execute()) { 
            echo json_encode("success");
        } else {
            echo json_encode("fail");
        }
});

?>

这是我的Android Side COde

活性

    public class DirectOrderActivity extends AppCompatActivity {

    EditText name,contact,address;
    Button buttonUpload;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_direct_order);

        name = (EditText) findViewById(R.id.input_name);
        address = (EditText) findViewById(R.id.input_address);
        contact = (EditText) findViewById(R.id.input_contact);
        buttonUpload = (Button) findViewById(R.id.btn_directorder);

        buttonUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ename=name.getText().toString();
                String eaddress=address.getText().toString();
                String econtact=contact.getText().toString();

                RestAdapter adapterCity = new RestAdapter.Builder()
                        .setEndpoint(Base.Base_Url)
                        .setLogLevel(RestAdapter.LogLevel.FULL)
                        .build();

                UserInterface services = adapterCity.create(UserInterface.class);
                services.directorder(ename , econtact,eaddress , new Callback<String>() {
                    @Override
                    public void success(String s, Response response) {
                        Toast.makeText(getApplicationContext(), "Order Successfully", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(getApplicationContext() , MainActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void failure(RetrofitError error) {

                    }
                });



            }
        });

    }
}

xml文件

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.anthesis.benchmarkayurveda.NewVisitActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="24dp"
        android:paddingRight="24dp">



        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:hint="Name of Docotr/Medical Shop" />
        </android.support.design.widget.TextInputLayout>


        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:hint="Address" />
        </android.support.design.widget.TextInputLayout>

        <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_contact"
                android:layout_below="@+id/input_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:hint="Contact"
                android:lines="1"/>
        </android.support.design.widget.TextInputLayout>


            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btn_directorder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_marginBottom="24dp"
                android:layout_marginLeft="20dp"
                android:padding="12dp"
                android:background="@color/colorPrimary"
                android:text="Save and Place Order"/>



    </LinearLayout>
</RelativeLayout>

INTERFACE

    public interface UserInterface {

    @POST("/directorder")
    public void directorder(@Query("d_name") String ename, @Query("d_contact") String eaddress, @Query("d_address") String econtact, retrofit.Callback<String> response);


}

帮帮我们..请

1 个答案:

答案 0 :(得分:0)

您应该发回正确的HTTP状态代码以确定失败。这是在Slim中通过返回您的路由可调用的$响应来完成的。

即。代码应该看起来像这样:

$app->post('/directorder',function($request) {

    require_once('dbconnect.php');

    $query = "INSERT INTO  direct_order (d_name,d_contact,d_address) values (?,?,?)";
    $stmt = $mysqli->prepare($query);

    $a = $request->getParsedBody()['d_name'];
    $b = $request->getParsedBody()['d_contact'];
    $c = $request->getParsedBody()['d_address'];
    $stmt->bind_param("sss",$a,$b,$c);


    if ($stmt->execute()) {
        $statusCode = 200;
        $output['result'] = 'success';
    } else {
        $statusCode = 500;
        $output['result'] = 'fail';
    }

    return $response->withJson($output, $statusCode);
});

请注意,如果您使用withJson,可以让Slim对JSON进行编码并为您设置正确的Content-Type标头,这会让生活变得更轻松。