I want to use the JSONParser codes but when I write the codes some imports are wrong or it's marked by red.What I have to do?
try using the volley library. this will remvoe all your headache of using the JSONParser class to get the json from the url and so on.
Please refer this link for volley:
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
.. Let me know if this helps ! :)
Have you added the dependency to your project? If not, write this line to your build.gradle file
compile 'org.apache.httpcomponents:httpcore:4.0-beta3'
For Http Client, add this dependency for your project:
compile 'org.apache.httpcomponents:httpclient:4.5.1'
Besides, that library was deprecated. You should use HttpUrlConnection instead.
TIP: If you want to import some library (dependency), just copy that library package(e.g. org.apache.http.HttpEntity) + "dependency" then type to Google :D
Try using Fast Android Networking Library
Add this dependency to your build.gradle (app level)
compile 'com.amitshekhar.android:android-networking:1.0.0'
Add internet permission in manifest
For GET Method
AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllUsers/{pageNumber}")
.addPathParameter("pageNumber", "0")
.addQueryParameter("limit", "3")
.addHeaders("token", "1234")
.setTag("test")
.setPriority(Priority.LOW)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
#Override
public void onResponse(JSONArray response) {
// do anything with response
}
#Override
public void onError(ANError error) {
// handle error
}
});
For POST method
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createAnUser")
.addBodyParameter("firstname", "Amit")
.addBodyParameter("lastname", "Shekhar")
.setTag("test")
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
// do anything with response
}
#Override
public void onError(ANError error) {
// handle error
}
});
documentation here: https://github.com/amitshekhariitbhu/Fast-Android-Networking
While adding JSONParser you may get some errors, kindly do this two step:
For me the path was like: C:\Users\vaidu\Instructable2\app\build.gardle
Add this two things to buildgradle file
android {
useLibrary 'org.apache.http.legacy'}
dependencies{ compile 'com.android.support:appcompat-v7:26.1.0' }
Related
I have synced and resynced the gradle files and built and rebuilt the app many times. I've closed android studio and even restarted the computer. Same error appears.
I've been following the aws tutorial and I'm on step five in the paragraph:
public void runMutation(){
CreateTodoInput createTodoInput = CreateTodoInput.builder().
name("Use AppSync").
description("Realtime and Offline").
build();
mAWSAppSyncClient.mutate(CreateTodoMutation.builder().input(createTodoInput).build())
.enqueue(mutationCallback);
}
private GraphQLCall.Callback<CreateTodoMutation.Data> mutationCallback = new GraphQLCall.Callback<CreateTodoMutation.Data>() {
#Override
public void onResponse(#Nonnull Response<CreateTodoMutation.Data> response) {
Log.i("Results", "Added Todo");
}
#Override
public void onFailure(#Nonnull ApolloException e) {
Log.e("Error", e.toString());
}
};
There is a red underline beneath CreateTodoInput, mutationCallback as well as several others.
Assume you should have GraphQL endpoint and API key after step 4.
Make sure you add classpath 'com.amazonaws:aws-android-sdk-appsync-gradle-plugin:2.9.+' to project/build.gradle and
add the below dependencies in app/build.gradle
apply plugin: 'com.amazonaws.appsync'
dependencies {
//Base SDK
implementation 'com.amazonaws:aws-android-sdk-core:2.15.+'
//AppSync SDK
implementation 'com.amazonaws:aws-android-sdk-appsync:2.8.+'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}
After step 4 (amplify push), make sure to BUILD your android project and SYNC all the gradle files
Here is the AWS Amplify tutorial page
I'm writing an Android application with lots of dependencies and I needed to enable multidex because it exceeds the limit (65536k methods). I followed this guide and able to compile and run the application on my test device. The problem is I'm getting a nullpointerexception in Robospice RequestListener
for every api call that I make. I'm sure that the api is responding. It seems that the app fails to map the response to my pojo class.
Note: It's running fine before I enabled the multidex.
here is the request listener
private class MyRequestListener implements RequestListener<PojoClass> {
#Override
public void onRequestFailure(SpiceException e) {
}
#Override
public void onRequestSuccess(PojoClass pojoClass) {
// pojoClass <---- this is null
}
}
here are my dependencies for rest and mapping
'org.springframework.android:spring-android-rest-template:2.0.0.M1'
'com.octo.android.robospice:robospice-spring-android:1.4.14'
'org.codehaus.jackson:jackson-core-asl:1.9.13'
'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
Any suggestions
I originally had ion-1.3.8.jar in my libs directory in Android Studio, with compile files('libs/ion-1.3.8.jar') in my build.gradle file. I also had (and still have) androidasync-1.3.8.jar in my libs folder and an entry for it in build.gradle.
I want to get rid of the ion jar and instead use a dependency, but when I delete the jar and put compile 'com.koushikdutta.ion:ion:2.+ in my build.gradle file, I get an error ("cannot resolve method get(String...))" on the following line of code in one of my classes: result.getHeaders().get("USER-TOKEN");.
I'm guessing that line of code probably wasn't what it should have been all along, but it did work with Ion 1.3.8. I can't figure out what I should change it to, and though I can probably figure it out with some trial and error, I would like to hear from the community or Koush about this.
Also, Android Studio is warning me that "Avoid using + in version numbers; can lead to unpredictable and unrepeatable builds". Any thoughts about this?
Here's my code:
Ion.with(activity)
.load(url)
.setJsonObjectBody(loginObject)
.asJsonObject()
.withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
#Override
public void onCompleted(Exception e, com.koushikdutta.ion.Response<JsonObject> result) {
String errorText = activity.getString(R.string.unable_to_login);
if (e != null) {
String errorMessage = e.getLocalizedMessage();
Log.e(TAG, errorMessage);
if (errorMessage.contains(activity.getString(R.string.unable_resolve_host))) {
errorText = activity.getString(R.string.no_internet);
}
}
else if (result != null) {
JsonElement reply = result.getResult().get(activity.getString(R.string.user));
if (reply != null) {
errorText = NO_ERROR;
SessionManager.setProfile(result.getResult());
String userToken = result.getHeaders().get(activity.getString(R.string.user_token));
SessionManager.createLoginSession(userToken);
if (SessionManager.isProfileComplete()) {
activity.startActivity(new Intent(activity, NavigationDrawerActivity.class));
}
else {
activity.startActivity(new Intent(activity, ProfileActivity.class));
}
}
else if (result.getResult().getAsJsonArray(activity.getString(R.string.errors)) != null) {
errorText = activity.getString(R.string.login_error);
}
}
else { // There was some other problem logging in
Log.e(TAG, "Unknown login problem");
}
requestCompleted.onRequestCompleted(errorText);
}
});
On version 2.+ of ION the API probably changed.
If you want to use the old API that works with your code, change your dependency to:
compile 'com.koushikdutta.ion:ion:1.3.8'
It will also automatically add the Android Async 1.3.8 dependency for you.
The warning saying that you should avoid the '+' is because that way, gradle will always get the newest version it finds, where some APIs can change and then break your code.
The API changed slightly in 2. One of those was changing the names of a few methods around the ResponseFuture and RawHeaders classes. In your case, getHeaders() was simply renamed to headers().
The transition from 1.x to 2.x is fairly painless, but there are some breaking changes nonetheless.
If you don't want to deal with the breaking changes, and use the latest version of 1.x in gradle, do the following:
compile 'com.koushikdutta.ion:ion:1.+
There are no breaking changes between 1.x versions. Current 1.x version is 1.4.1 I believe.
I cloned latest version of volley from https://android.googlesource.com/platform/frameworks/volley
I imported it in Eclipse, and tried to run the test project, but I get ClassNotFoundException
java.lang.NoClassDefFoundError: com.android.volley.mock.WaitableQueue$MagicStopRequest
at com.android.volley.mock.WaitableQueue.<init>(WaitableQueue.java:31)
at com.android.volley.CacheDispatcherTest.setUp(CacheDispatcherTest.java:45)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619)
How do I run theses tests?
Thank you.
The Volley framework repo comes with support for both Ant and Gradle based build systems. Running the tests using the Gradle build system is quite simple. I did briefly try using the Ant approach, but it wasn't as straight forward and I prefer Gradle anyhow.
First thing you'll need is to install Gradle 1.10, which is the version required by Volley as of writing this answer. Alternatively, you could clone this mirror repo, which includes some extras, such as gradlew support (Gradle bootstrapping utility for downloading and using the proper version of Gradle).
Then simply execute the following command from the project root (using either gradle or gradlew based on which option you chose above):
$> gradle clean connectedCheck
This will run the full gambit of tasks, including the tests. It should produce an HTML test report at build/reports/instrumentedTests/connected/index.html, relative to the project root.
I found this to get start with.
I write this Test Project , and import com.android.volley and com.android.volley.toolbox from the volley library. And it works ^_^
public class MainActivity extends Activity {
protected static final String TAG = "com.gyh.myvolleytest";
private static Response.ErrorListener createErrorListener() {
return new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error Response code: " + error.getMessage());
}
};
}
private static Response.Listener<String> createSuccessListener() {
return new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// TODO parse response
String string = response.toString();
Log.d(TAG, "string :"+string);
}
};
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//it is the only button in the layout ,click to log the result
public void click(View view) {
RequestQueue queue = Volley.newRequestQueue(this);
String url="http://192.168.1.108:8080/httptest/servlet/mainservlet?name=stack&age=23";
StringRequest request = new StringRequest(
Request.Method.GET,
url,
createSuccessListener(),
createErrorListener());
queue.add(request);
queue.start();
}
}
let me know what happens ^_^
v1.addView(new TransfomedViewWidget(<-- (9)
this,
new Transformation() {<-- (10)
#Override public String describe() { return "rotate(-30)"; }
#Override public void transform(Canvas canvas) {
canvas.rotate(-30.0F);
} }));
I am new to Java and android programming , can anyone tell me what this <-- operator means? I am getting error while trying to compile this in eclipse.
It isn't part of the syntax, it loks like something else has put it there.
Certainly it was a note or comment ... add // in front of them and try to compile again.