Czesc,
Staram sie pobrac jednego Stringa z bazy danych i wyswietlic to w androidzie, niestety wyskakuje
E/blad: getting data failed.java.io.EOFException: End of input at line 1 column 1 path $
API
<?php
require_once "conn.php";
$pg_qry="SELECT login FROM register WHERE id=62";
$result=pg_query($conn,$pg_qry);
$wiersz = pg_fetch_array( $result);
if($result)
{
json_encode($wiersz['login']);
}
else
{
json_encode ("getdata not success");
}
pg_close();
?>
MAIN Mowimy o metodzie getdata()
probowalem tez showResponse(response.body().toString()); ale ten sam blad wyskakuje
public class MainActivity extends AppCompatActivity {
private Post logp;
EditText log, pass;
Button przycisk;
TextView mResponseTv;
private APIService mAPIService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
log = (EditText) findViewById(R.id.loginetid);
pass = (EditText) findViewById(R.id.passwordetid);
mResponseTv = (TextView) findViewById(R.id.napisid);
Button submitBtn = (Button) findViewById(R.id.button);
mAPIService = ApiUtils.getAPIService();
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String login = log.getText().toString().trim();
String password = pass.getText().toString().trim();
if (!TextUtils.isEmpty(login) && !TextUtils.isEmpty(password)) {
sendPost(login, password);
}
}
});
}
public void sendPost(String login, String password) {
Call<Post> call = mAPIService.savePost(login, password, 2);
call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (response.isSuccessful()) {
showResponse(response.body().toString());
Log.i("wyslane", "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
Log.e("blad", "Unable to submit post to API." + t);
}
});
}
public void getData() {
mAPIService.getUserInfo().enqueue(new Callback<Post>() {
@Override
public void onResponse(@NonNull Call<Post> call, @NonNull Response<Post> response) {
showResponse(response.body().getLogin());
Log.i("wyslane", "getting data success." + response.body().toString());
}
@Override
public void onFailure(@NonNull Call<Post> call, @NonNull Throwable t) {
Log.e("blad", "getting data failed." + t);
}
});
}
public void readBt(View v) {
getData();
}
public void showResponse(String response) {
if (mResponseTv.getVisibility() == View.GONE) {
mResponseTv.setVisibility(View.VISIBLE);
}
mResponseTv.setText(response);
}
}
POST
public class Post {
@SerializedName("login")
@Expose
private String login;
@SerializedName("password")
@Expose
private String password;
@SerializedName("id")
@Expose
private Integer id;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Post{" +
"login='" + login + '\'' +
", password='" + password + '\'' +
", id=" + id +
'}';
}
}
retofitclient
package com.example.ad.retrofittest;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by ad on 2018-01-16.
*/
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
ApiUtils
package com.example.ad.retrofittest;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by ad on 2018-01-16.
*/
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
ApiService
package com.example.ad.retrofittest;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
/**
* Created by ad on 2018-01-16.
*/
public interface APIService {
@GET("login.php")
Call<Post> getUserInfo();
@POST("register.php")
@FormUrlEncoded
Call<Post> savePost(@Field("login") String login,
@Field("password") String password,
@Field("id") long id);
}
wydaje mi sie ze blad lezy po stronie API
aczkolwiek jak wypisze sobie zmienna $wiersz['login'] w przegladarce to pobiera i wyswietla poprawna wartosc i nie ma zadnych bledow
Jakies pomysly?