通过okHttp3, 输入URL ,返回String类型数据

 

/**
 * 通过okHttp3 请求网络数据,返回String类型数据
 */

public class GetJsonData {
    private static String TAG = "TESTJSON";
    private static String data = "";

    public static String getJsonData(String urlStr) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(urlStr)
                .build();
        Call call = client.newCall(request);

        try {
            Response response = call.execute();
            data = response.body().string();
            Log.i(TAG, "获取数据成功");
            Log.i(TAG, "response code =" + response.code());
            Log.i(TAG, "response body = " + data);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return data;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: 1. 首先,在你的 build.gradle 文件中添加 OkHttp 的依赖: ``` implementation 'com.squareup.okhttp3:okhttp:4.9.0' ``` 2. 在布局文件中,创建一个包含用户名和密码输入框、登录按钮的视图: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/usernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码" /> <EditText android:id="@+id/passwordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" /> <Button android:id="@+id/loginButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout> ``` 3. 在 Activity 中,获取输入框和登录按钮的引用,并设置按钮的点击事件: ```java public class LoginActivity extends AppCompatActivity { private EditText usernameEditText, passwordEditText; private Button loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameEditText = findViewById(R.id.usernameEditText); passwordEditText = findViewById(R.id.passwordEditText); loginButton = findViewById(R.id.loginButton); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); } private void login() { String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); // TODO: 发送网络请求 } } ``` 4. 在 login() 方法中,使用 OkHttp 发送网络请求,并解析服务器返回的 JSON 数据: ```java private void login() { String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("username", username) .add("password", password) .build(); Request request = new Request.Builder() .url("http://example.com/login") .post(requestBody) .build(); try { Response response = client.newCall(request).execute(); String responseData = response.body().string(); // TODO: 解析服务器返回的 JSON 数据 } catch (IOException e) { e.printStackTrace(); } } ``` 这里的 URL 和参数只是示例,请根据实际情况替换成你自己的服务器地址和参数。至于如何解析 JSON 数据,可以使用 Android 自带的 JSONObject 或者第三方库如 Gson 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值